android Widget 桌面小图标,类似快捷方式
2014-04-07 19:56
357 查看
步骤一:清单文件配置
receiver必须是AppWidgetProvider的子类
通过intent-filter过滤指定的android.appwidget.action.APPWIDGET_UPDATE动作
除此之外,还需配置meta-data属性,其中name为指定字段,
resource为欲添加小图标时显示的样式
//resource配置模板
/**
*minWidth 桌面显示的最小宽度
*minHeight 桌面显示的最小高度,以上两个参数决定改桌面图标的风格
*initialLayout 图标显示时加载的布局文件,可以是指定宽高的任意布局文件,但建议尽量简单
process_widget.xml为自定义的布局
*/
步骤二:建立ProcessReceiver,即AppWidgetProvider的子类,用于处理相应的业务
示例为一个清理进程服务的启动为停止
步骤三:桌面图标的业务处理
1,得到AppWidgetManager实例,如同activityManager与PackageManager一样
2,建立组件,即将服务与图标建立连接
3,建立RemoteViews对象,即可操作图标上的子组件
与普通activity不同的是,这里不能findView,而只能通过setTextViewText
之类操作子组件。
4,通过PendingIntent发出广播,设置相应的action
setOnClickPendingIntent为子组件的点击事件注册点击处理逻辑
最后交给广播接收者处理业务逻辑
receiver必须是AppWidgetProvider的子类
通过intent-filter过滤指定的android.appwidget.action.APPWIDGET_UPDATE动作
除此之外,还需配置meta-data属性,其中name为指定字段,
resource为欲添加小图标时显示的样式
<receiver android:name=".Receiver.ProcessReceiver"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/process_widget"></meta-data> </receiver> <receiver android:name=".Receiver.KillProcessReceiver"> <intent-filter> <action android:name="cn.gzu.edu.action.killprocess"></action> </intent-filter> </receiver>
//resource配置模板
/**
*minWidth 桌面显示的最小宽度
*minHeight 桌面显示的最小高度,以上两个参数决定改桌面图标的风格
*initialLayout 图标显示时加载的布局文件,可以是指定宽高的任意布局文件,但建议尽量简单
process_widget.xml为自定义的布局
*/
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="290dp" android:minHeight="72dp" android:initialLayout="@layout/process_widget" > </appwidget-provider>
步骤二:建立ProcessReceiver,即AppWidgetProvider的子类,用于处理相应的业务
示例为一个清理进程服务的启动为停止
public class ProcessReceiver extends AppWidgetProvider { @Override public void onEnabled(Context context) { super.onEnabled(context); Intent intentStart=new Intent(context,ProcessWidgetService.class); context.startService(intentStart); } @Override public void onDeleted(Context context, int[] appWidgetIds) { super.onDeleted(context, appWidgetIds); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); } @Override public void onDisabled(Context context) { super.onDisabled(context); Intent intentStop=new Intent(context,ProcessWidgetService.class); context.stopService(intentStop); } }
步骤三:桌面图标的业务处理
1,得到AppWidgetManager实例,如同activityManager与PackageManager一样
2,建立组件,即将服务与图标建立连接
3,建立RemoteViews对象,即可操作图标上的子组件
与普通activity不同的是,这里不能findView,而只能通过setTextViewText
之类操作子组件。
4,通过PendingIntent发出广播,设置相应的action
setOnClickPendingIntent为子组件的点击事件注册点击处理逻辑
最后交给广播接收者处理业务逻辑
public class ProcessWidgetService extends Service { private Timer timer; private TimerTask task=new TimerTask(){ @Override public void run() { AppWidgetManager awManager=AppWidgetManager.getInstance(getApplicationContext()); ComponentName provider=new ComponentName(getApplicationContext(), ProcessReceiver.class); RemoteViews remoteViews=new RemoteViews(getPackageName(), R.layout.process_widget); int processSize=TaskProcessUtil.getRunningAppSize(getApplicationContext()); remoteViews.setTextViewText(R.id.process_count, "运行总进程数:"+processSize+"个"); long memory=TaskProcessUtil.getSystemAviMemory(getApplicationContext()); remoteViews.setTextViewText(R.id.process_memory, "剩余内存:"+TextFormat.formatByte(memory)); Intent intent=new Intent(getApplicationContext(),KillProcessReceiver.class); intent.setAction("cn.gzu.edu.action.killprocess"); PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(), 100, intent, 0); remoteViews.setOnClickPendingIntent(R.id.process_widget_btn_clear, pendingIntent); awManager.updateAppWidget(provider, remoteViews); }}; @Override public void onCreate() { super.onCreate(); //不断的对widget里面的数据进行更新 用定时器来实现 timer=new Timer(); timer.schedule(task,0 ,1000); } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { super.onDestroy(); task.cancel(); task=null; }
相关文章推荐
- Android 添加、移除和判断 桌面快捷方式图标
- <android>桌面快捷方式 + 桌面动态图标(展示未读信息条数) + 悬浮窗 + 沉浸式状态栏
- Android APK安装后不在桌面生成快捷方式图标
- Android APP自动生成桌面快捷图标 启动方式有差异
- Android桌面快捷方式图标生成与删除,使用Intent与launcher交互
- Android桌面快捷方式图标生成与删除,使用Intent与launcher交互
- 在Android桌面添加应用程序的快捷方式图标
- 如何在android桌面添加几个app图标做快捷方式?
- Android桌面快捷方式图标生成与删除,使用Intent与launcher交互
- Android桌面快捷方式图标生成与删除,使用Intent与launcher交互
- Android桌面快捷方式图标生成与删除,使用Intent与launcher交互
- android 桌面快捷方式、窗口小部件、文件夹、书签 launcher、appwidget、shortcut、folder、bookmark
- 如何在android桌面添加几个app图标做快捷方式
- Android桌面快捷方式图标生成与删除,使用Intent与launcher交互
- Android 添加、移除桌面快捷方式图标
- android 预置桌面上的应用程序图标、快捷方式图标或者窗口小部件
- Android 如何预置桌面上的应用程序图标、快捷方式图标或者窗口小部件?
- Android桌面组件widget与快捷方式shortcut
- 解析Android应用启动后自动创建桌面快捷方式的实现方法
- win8桌面图标快捷方式小箭头快速去除方法