Android Custom Launcher adds a custom widget and cannot update the widget interface. Custom widgets can be updated on the system launcher. System widgets such as clocks can be updated on the custom launcher.
AppWidgetProvider code is as follows:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
String dateStr = dateFormat.format(new Date());
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setTextViewText(R.id.tv_update, dateStr);
Log.i("TAG","--"+dateStr);
appWidgetManager.updateAppWidget(new ComponentName(context, WidgetProvider.class),views);
}
The code related to the custom launcher is as follows
It is created in onCreate,
//得到com.test包名的所有widegt,并且分配一个appwidgetId,保存起来
mAppWidgetHost = new AppWidgetHost(MainActivity.this, HOST_ID);
mAppWidgetHost.startListening();
mAppWidgetManager = AppWidgetManager.getInstance(MainActivity.this);
List<AppWidgetProviderInfo> providers = mAppWidgetManager.getInstalledProviders();
for (int i = 0; i < providers.size(); i++) {
ComponentName provider = providers.get(i).provider;
if (provider.getPackageName().contains("com.test")) {
surfAppWidgetList.add(providers.get(i));
surfAppWidgetIdList.add(mAppWidgetHost.allocateAppWidgetId());
LogUtil.printlf("-->" + provider.getPackageName());
}
}
//intent里面可以传入
mAppWidgetIds=new int[surfAppWidgetIdList.size()];
Then add it to the parent layout in a loop
AppWidgetHostView hostView = mAppWidgetHost.createView(this, surfAppWidgetIdList.get(i), surfAppWidgetList.get(i));
addView(hostView);
What is the problem?
Already solved. Need to perform bindAppWidgetId();
And bindAppWidgetId requires system permissions. You must upgrade your own application to a system application.