代码改变世界

appWidget 简单入门学习笔记

  youxiachai  阅读(5829)  评论(0编辑  收藏  举报

1,让桌面能够添加你的appWidget

1,一个类,和2个xml

1
2
3
4
//一个实现AppWidgetProvider的类
public class TomAppWidgetProvider extends AppWidgetProvider {
 
}
1
2
3
4
5
6
7
8
9
10
<!--放在 res/xml 文件夹中命名为tom_appwidget_info.xml-->
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="86400000"
    android:initialLayout="@layout/tom_appwidget"
    android:configure="kg.tom.AppWidgetConfigure"
    >
</appwidget-provider>
<!-- android:initialLayout: 初始化你的appWidget布局 -->

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!--放置在res/layout/tom_appwidget_provider.xml-->
<?xml version="1.0" encoding="utf-8"?>
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="294dp"
    android:layout_height="72dp"
    android:orientation="vertical"
    >
<TextView
    android:id="@+id/appwidget_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#ff000000"
/>
    <Button
        android:id="@+id/appwidget_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@android:string/ok"
    />
 
</LinearLayout>

在AndroidManifest.xml中声明你的appWidget

1
2
3
4
5
6
7
8
9
<receiver android:name="TomAppWidgetProvider" >
    <intent-filter>
<!--让系统能够设别到你的appWidgetProvider的动作-->
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
<!--设置你的appWidget的布局-->
    <meta-data android:name="android.appwidget.provider"
               android:resource="@xml/tom_appwidget_info" />
</receiver>

 

长按你的Home 界面,你就会看到你的widget已经在列表当中

image

但是,现在点击是会出错的,这时候我们需要设置我们的AppWidgetConfigure

在activity中增加一个Intent-filter

1
2
3
4
<intent-filter>
            <!-- 让系统能够设别到你的appWidgetProvider的动作 -->
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class AppWidgetConfigure extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置widgetId
        int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
        //1,将setResult 设置为 取消,用于取消widget host
        setResult(RESULT_CANCELED);       
        //2,从Intent 中找到widget 的id
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if(extras != null){
            mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
        }
         
        //3,如果获取不到 appWidget id 我们就结束
        if(mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID){
            Log.d("app", "获取失败退出!!!");
            finish();
        }
         
         
        final Context context = AppWidgetConfigure.this;
        //4,实例化你的 appWidget
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
         
        //5,更新widget
        Intent resultValue = new Intent();
        resultValue.putExtra(appWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
        setResult(RESULT_OK, resultValue);
        finish();
         
         
    }
}

 

2,然后写provider的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class TomAppWidgetProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        final int N = appWidgetIds.length;
        Log.d("app", "onUpdate--->Ids==="+String.valueOf(N) );
        for(int i=0; i < N; i++){
            int appWidgetId = appWidgetIds[i];
             
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
         
    }
     
    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,int appWidgetId){
        Log.d("app", "update---->id" + appWidgetId);
        //1,widget中的的标题
        CharSequence text = "这是我第一个widget";
        //2,widget显示用布局,并设置text显示的值
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.tom_appwidget_provider);
        views.setTextViewText(R.id.appwidget_text, text);
        //3,通知widget manager 更新
        appWidgetManager.updateAppWidget(appWidgetId, views);
         
    }
     
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onDeleted(context, appWidgetIds);
        //删除的时候调用的方法
        int appids = appWidgetIds.length;
        Log.d("app", "onDelete--->" + appids);
    }
}

 

然后就可以运行了:

image

顺便附上一张简单原理图:..

appWidget_simple

2,小小的进阶为widget实现运行activity

只要在加上几行代码的appwidget 就能实现挑战到Activity的功能

1,新建一个HelloAppWidget的activity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,int appWidgetId){
        Log.d("app", "update---->id" + appWidgetId);
        //1,设置显示用标题
        CharSequence text = "这是我第一个widget";
         
        //1.1,增加跳转用activity相关 intent
        Intent intent = new Intent(context, HelloAppWidget.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        //2,如果,没有在xml中声明RemoteViews布局,这里就必须要让其他布局基于RemoteViews
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.tom_appwidget_provider);
        views.setTextViewText(R.id.appwidget_text, text);
        //2.1将需要跳转的intent绑定到appWidget button中
        views.setOnClickPendingIntent(R.id.appwidget_button, pendingIntent);
         
        //3,通知widget manager 更新
        appWidgetManager.updateAppWidget(appWidgetId, views);
         
    }

image 跳转到特定activity…ps:使用activity记得在AndroidManifest.xml中注册

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示