定制android桌面系统
2017-11-27 17:22
330 查看
最近项目用到桌面定制,于是研究了AppWidgetProvider,话不多说,直接进入正题.
其实打开AppWidgetProvider源码我们会发现,他是继承于broadcastReceiver的广播接收器,其中会定义一个onUpdate的方法,该方法会在桌面加载或者更新的时候调用,里面定义了三个参数,分别是context,appWidgetManager和appWidgetIds,其中appWidgetsIds是桌面ID.而且此时我们需要开启一个服务,定时的更新桌面按钮触发事件.
其实打开AppWidgetProvider源码我们会发现,他是继承于broadcastReceiver的广播接收器,其中会定义一个onUpdate的方法,该方法会在桌面加载或者更新的时候调用,里面定义了三个参数,分别是context,appWidgetManager和appWidgetIds,其中appWidgetsIds是桌面ID.而且此时我们需要开启一个服务,定时的更新桌面按钮触发事件.
@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Log.d(TAG, " ------ onUpdate(): appWidgetIds.length=" + appWidgetIds.length); // 每次 widget 被创建时,对应的将widget的id添加到set中 for (int appWidgetId : appWidgetIds) { idsSet.add(Integer.valueOf(appWidgetId)); } prtSet(context); // 解决CashierWidgetProvider没有定时刷新问题(不刷新则不会变化layout 已经按钮触发事件) Log.d(TAG, " ------ onUpdate(): CashierWidgetService"); Intent intent = new Intent(context, MyWidgetService.class); PendingIntent refreshIntent = PendingIntent.getService(context, 0, intent, 0); AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarm.setRepeating(AlarmManager.RTC, 0, 1000, refreshIntent); context.startService(intent); } 在onReceive里会接收服务发来的广播,此时我们需要更新桌面,并对刷新按钮的触发事件,如果广播接收的action是Intent.CATEGORY_ALTERNATIVE 此时可根据data.getSchemeSpecificPart()做相应的点击事件
@Override public void onReceive(Context context, Intent intent) { if (context == null) { return; } final String action = intent.getAction(); Log.d(TAG, " ------ OnReceive:Action: " + action); if (ACTION_UPDATE_ALL.equals(action)) { // “更新”广播,当覆盖安装时:AppWidgetManager.getInstance(context)会报空指针 updateAllAppWidgets(context, AppWidgetManager.getInstance(context), idsSet); } else if (intent.hasCategory(Intent.CATEGORY_ALTERNATIVE)) { // “按钮点击”广播 Uri data = intent.getData(); int clickId = Integer.parseInt(data.getSchemeSpecificPart()); Log.d(TAG, " ------ OnReceive:clickId: " + clickId); switch (clickId) { case REQUEST_CLICK_RL0_EVENT: startPetApp(context, Activity_One.class); break; case REQUEST_CLICK_RL1_EVENT: startPetApp(context, Activity_Two.class); break; case REQUEST_CLICK_RL2_EVENT: startPetApp(context, Activity_One.class); break; case REQUEST_CLICK_RL3_EVENT: startPetApp(context, Activity_Two.class); break; case REQUEST_CLICK_RL4_EVENT: startPetApp(context, Activity_One.class); break; case REQUEST_CLICK_RL5_EVENT: startPetApp(context, Activity_Two.class); break; case REQUEST_CLICK_RL6_EVENT: startPetApp(context, Activity_One.class); break; default: break; } } super.onReceive(context, intent); } 源码地址 https://github.com/kyrieLiu/petWidget1.git 运行demo方法:运行后长按桌面,点击小组件,找到桌面dmeo,把它拖到桌面上来,然后可以查看效果
相关文章推荐
- 深入浅出 - Android系统移植与平台开发(六)- 为Android启动加速 + 定制桌面
- Android之UncaughtExceptionHandler定制自己的错误日志系统
- 定制你自己的android手机桌面Launcher===一个完成Launcher开发的介绍 (第二篇)
- android系统旋转方向定制问题
- 手把手教你深度定制tiny4412安卓5.0系统(一)---开发板如何预置文件到android系统
- Android性能优化之UncaughtExceptionHandler定制自己的错误日志系统
- 详解android系统的定制
- Live-Android,让你在桌面系统下体验Android
- Android 5.0 Launcher客制化定制之 桌面锁屏
- 显示Android系统桌面
- Android UI设计——定制桌面
- android 系统定制的小技巧(网络收集)
- android系统旋转方向定制问题
- Android系统编译之将自己编的Launcher APK成为系统桌面应用
- 定制替换Android桌面(home screen)
- (11)Android Launcher 定制之4.4最新桌面 单层,双层切换功能
- 去除Android系统中桌面顶部阴影
- Live-Android,让你在桌面系统下体验Android
- 谷歌Android系统十大热门关键词 刷机 桌面widget WIFI