android创建应用小挂件

您所在的位置:网站首页 安卓手机桌面日历挂件怎么设置 android创建应用小挂件

android创建应用小挂件

2024-06-06 01:17| 来源: 网络整理| 查看: 265

一.先演示进入前不进行配置的小挂件 1.首先在 res/layout/下 创建小挂件布局文件 appinfowidget.xml,如下: 在这里插入图片描述

在这里插入图片描述

2.创建AppWidgetProviderInfo 对象 描述应用微件的元数据,如应用微件的布局、更新频率和 AppWidgetProvider 类。此对象应在 XML 中定义。如下:在res/xml目录下创建 appinfo.xml 在这里插入图片描述

appwidget-provider的其余属性都可在官网查询到,注意这里没有加 android:configure=“com.example.myfirstapplication.ConfigureActivity”。如果不想在手机上添加widget时进行配置就不要加入这句,不然在添加widget时,会自动跳转到ConfigureActivity对应的配置界面。在这个演示中不加

3.在AndroidManifest.xml文件的标签下 添加一个标签,如下:

功能介绍:首先是一个过滤器,当发送的intent包含该action时,才会调用类.ReponseApp(该类正是第4步要创建的)中的onRceiver()回调方法,至于action为什么是"android.appwidget.action.APPWIDGET_UPDATE",后面分析源码的时候再说。 而中可以看成一片存放了数据的区域,其官网说明:可以向父组件提供的其他任意数据项的名称值对。一个组件元素可以包含任意数量的 子元素。所有这些子元素的值收集到一个 Bundle 对象,并且可作为 PackageItemInfo.metaData 字段提供给组件。普通值通过 value 属性指定。不过,要将资源 ID 指定为值,请改为使用 resource 属性。例如,以下代码会将 @string/kangaroo 资源中存储的任何值分配给“zoo”名称:

另一方面,使用 resource 属性会为资源的数字 ID 分配“zoo”,而不是资源中存储的值:

显然,由于resource引用的是元数据信息,故可以很容易获取应用挂件的一些信息

4.AppWidgetProvider 类实现 定义允许您基于广播事件以编程方式与应用微件连接的基本方法。通过它,您会在更新、启用、停用和删除应用微件时收到广播。 创建一个继承了AppWidgetProvider的类ReponseApp,如下:

public class ReponseApp extends AppWidgetProvider { //接收到ACTION_APPWIDGET_UPDATE活动的广播时调用 @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Toast.makeText(context,"onUpdate被调用",Toast.LENGTH_SHORT).show(); //每次在手机上添加一个widget就会有一个appWidgetId(如何在手机上添加widget后面再说) final int N=appWidgetIds.length; //如果添加了多个widget,就更新每一个widget for(int i=0;i 0) { this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds); } } } else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) { Bundle extras = intent.getExtras(); if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) { final int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID); this.onDeleted(context, new int[] { appWidgetId }); } } else if (AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED.equals(action)) { Bundle extras = intent.getExtras(); if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID) && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS)) { int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID); Bundle widgetExtras = extras.getBundle(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS); this.onAppWidgetOptionsChanged(context, AppWidgetManager.getInstance(context), appWidgetId, widgetExtras); } } else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) { this.onEnabled(context); } else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) { this.onDisabled(context); } else if (AppWidgetManager.ACTION_APPWIDGET_RESTORED.equals(action)) { Bundle extras = intent.getExtras(); if (extras != null) { int[] oldIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_OLD_IDS); int[] newIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS); if (oldIds != null && oldIds.length > 0) { this.onRestored(context, oldIds, newIds); this.onUpdate(context, AppWidgetManager.getInstance(context), newIds); } } } }

而其他几个函数都是空的,由于AppWidgetProvider继承了BroadcastReceiver,可以知道他实际上就是一个广播,在回调方法onReceive()中可以看到当action为ACTION_APPWIDGET_UPDATE,并且在手机上创建了widget时,就会执行onUpdate方法。 而在这里插入图片描述 因此,在第3步的过滤器中action应该为android.appwidget.action.APPWIDGET_UPDATE 如果想调用onDelete()方法,根据onReceive()可知,在action为ACTION_APPWIDGET_DELETED时调用onDelete(), 在这里插入图片描述 故可以在过滤器中添加该action。

5.最终演示结果: 在手机上添加widget,如图:长按桌面空白处,点击添加插件,选择要添加的小插件即可。

在这里插入图片描述 就添加这个薯片样的 在这里插入图片描述 长按可以修改大小 在这里插入图片描述 点击左边第一个“安我呀”按钮可以跳转到主界面 在这里插入图片描述

二.演示进入前进行配置的小挂件

5.在第2步中appinfo.xml加上android:configure=“com.example.myfirstapplication.ConfigureActivity” (com.example.myfirstapplication换成你自己的包名) 如图: 在这里插入图片描述

创建配置页面布局activityconfigure.xml 在这里插入图片描述

创建ConfigureActivity.java

public class ConfigureActivity extends AppCompatActivity { int mAppWidgetID; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_configure); //First, get the App Widget ID from the Intent that launched the Activity //首先,从启动这个Activity的intent获得App Widget ID Intent intent=getIntent(); Bundle extras=intent.getExtras(); if(extras!=null){ mAppWidgetID= extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID); } //获取按钮 Button launchBtn=findViewById(R.id.LaunchBtn); Toast.makeText(ConfigureActivity.this,"请点击按钮",Toast.LENGTH_SHORT).show(); launchBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Perform your App Widget configuration. //When the configuration is complete, get an instance of the AppWidgetManager by calling getInstance(Context): //执行App Widget配置,当配置完成时,获取AppwidgetManager的实例 AppWidgetManager appWidgetManager= AppWidgetManager.getInstance(ConfigureActivity.this);//AppWidgetManager是一个公用的 //Update the App Widget with a RemoteViews layout by calling updateAppWidget(int, RemoteViews) //更新App Widget 视图 RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.appinfowidget); appWidgetManager.updateAppWidget(mAppWidgetID,remoteViews); //Finally, create the return Intent, set it with the Activity result, and finish the Activity //最后,创建intent,设置Activity结果,并结束Activity Intent resultintent=new Intent(); resultintent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,mAppWidgetID); setResult(RESULT_OK,resultintent); finish(); } }); } }

最终演示结果: 在点击添加小挂件时,会跳转到配置界面 在这里插入图片描述 点击配置完成后,就结束了,但是在点击挂件的“安我呀”按钮时,发现没有反应,以上基本都是按照官网上做的 下面对ConfigureActivity添加如下代码

//创建一个intent去启动MainActivity Intent intent=new Intent(ConfigureActivity.this,MainActivity.class); PendingIntent pendingIntent=PendingIntent.getActivity(ConfigureActivity.this,0,intent,0); remoteViews.setOnClickPendingIntent(R.id.play_button,pendingIntent);

总的代码如下:

public class ConfigureActivity extends AppCompatActivity { int mAppWidgetID; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_configure); //First, get the App Widget ID from the Intent that launched the Activity //首先,从启动这个Activity的intent获得App Widget ID Intent intent=getIntent(); Bundle extras=intent.getExtras(); if(extras!=null){ mAppWidgetID= extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID); } //获取按钮 Button launchBtn=findViewById(R.id.LaunchBtn); Toast.makeText(ConfigureActivity.this,"请点击按钮",Toast.LENGTH_SHORT).show(); launchBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Perform your App Widget configuration. //When the configuration is complete, get an instance of the AppWidgetManager by calling getInstance(Context): //执行App Widget配置,当配置完成时,获取AppwidgetManager的实例 AppWidgetManager appWidgetManager= AppWidgetManager.getInstance(ConfigureActivity.this);//AppWidgetManager是一个公用的 //Update the App Widget with a RemoteViews layout by calling updateAppWidget(int, RemoteViews) //更新App Widget 视图 RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.appinfowidget); //创建一个intent去启动MainActivity,新添加的代码 Intent intent=new Intent(ConfigureActivity.this,MainActivity.class); PendingIntent pendingIntent=PendingIntent.getActivity(ConfigureActivity.this,0,intent,0); remoteViews.setOnClickPendingIntent(R.id.play_button,pendingIntent); appWidgetManager.updateAppWidget(mAppWidgetID,remoteViews); //Finally, create the return Intent, set it with the Activity result, and finish the Activity //最后,创建intent,设置Activity结果,并结束Activity Intent resultintent=new Intent(); resultintent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,mAppWidgetID); setResult(RESULT_OK,resultintent); finish(); } }); } }

接下来就可以正常运行了



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3