1.創(chuàng)建AppWidget
AppWidget和Launcher,在早期Android被用的很泛濫。最近產品有需求,要給應用加快捷入口,想起來可以用AppWidget。
創(chuàng)建AppWidget可以參考官方文檔:
https://developer.android.com/guide/topics/appwidgets/overview
使用方式參考官方Demo:
https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/appwidget
完成功能的時候建議參考官方Demo
2.自動創(chuàng)建AppWidget
AppWidget屬于用戶低頻使用功能,等用戶自己去添加功能,可能性不大。如果能引導用戶,幫用戶創(chuàng)建會有更好的效果。
Android官方提供了自動創(chuàng)建的方法,但需要Android系統(tǒng)版本不低于8.0(API 26及以上)。
AppWidgetManager appWidgetManager =context.getSystemService(AppWidgetManager.class);
ComponentName myProvider =new ComponentName(context, MyAppWidgetProvider.class);
//@RequiresApi(api = Build.VERSION_CODES.O)
if (appWidgetManager.isRequestPinAppWidgetSupported()) {
// AppWidget創(chuàng)建成功回調
Intent pinnedWidgetCallbackIntent = new Intent(context, MyAppWidgetProvider.class);
PendingIntent successCallback = PendingIntent.getBroadcast(context, 0, pinnedWidgetCallbackIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//如果不需要創(chuàng)建成功回調,requestPinAppWidget的第三個參數可以為null
appWidgetManager.requestPinAppWidget(myProvider, null, successCallback);
}
我們應用8.0及以上的用戶覆蓋率超過90%,8.0以下的用戶忽略自動創(chuàng)建功能。