由网友(╱℡稻草人)分享简介:我有这个简单的小工具,当我点击它,它应该打开我的活动,它的工作原理,但它不会重新启动后正常工作。我必须删除,然后重新添加小部件到我的主屏幕,因为当我点击小窗口小部件不响应,不打开我的活动。那么问题出在哪里?code: 公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetMan...![汽车行业研究 景气周期再延续,供需两旺新机遇](https://p.xsw88.cn/allimgs/daicuo/20230904/8884.png)
我有这个简单的小工具,当我点击它,它应该打开我的活动,它的工作原理,但它不会重新启动后正常工作。我必须删除,然后重新添加小部件到我的主屏幕,因为当我点击小窗口小部件不响应,不打开我的活动。那么问题出在哪里?
code:
公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
最终诠释N = appWidgetIds.length;
的for(int i = 0; I&n种;我++){
INT appWidgetId = appWidgetIds [I]
context.startService(新意图(背景下,WidgetService.class));
意向意图=新的意图(背景下,WidgetDialog.class);
PendingIntent pendingIntent = PendingIntent.getActivity(上下文,0,意图,0);
RemoteViews意见=新RemoteViews(context.getPackageName(),R.layout.widget);
views.setOnClickPendingIntent(R.id.layout_widget,pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId,意见);
}
}
@覆盖
公共无效onEnabled(上下文的背景下){
context.startService(新意图(背景下,WidgetService.class));
}
@覆盖
公共无效onDisabled(上下文的背景下){
context.stopService(新意图(背景下,WidgetService.class));
}
解决方案
重启后你OnEnabled被称为替代的onupdate所以从的onupdate移动code到一个单独的方法,然后调用该方法,从更新所有部件onEnabled
I have this simple widget and when I click on it, it should open my activity, it works, but it doesn't work after reboot. I must delete and then again add widget to my homescreen, because when I click on widget widget doesn't respond and doesn't open my activity. So where is the problem?
![汽车行业研究 景气周期再延续,供需两旺新机遇](https://p.xsw88.cn/allimgs/daicuo/20230904/8884.png)
Code:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for(int i=0; i<N; i++){
int appWidgetId = appWidgetIds[i];
context.startService(new Intent(context, WidgetService.class));
Intent intent = new Intent(context, WidgetDialog.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.layout_widget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@Override
public void onEnabled(Context context){
context.startService(new Intent(context, WidgetService.class));
}
@Override
public void onDisabled(Context context){
context.stopService(new Intent(context, WidgetService.class));
}
解决方案
After reboot you OnEnabled is called instead of onUpdate so move the code from onUpdate into a separate method and then call that method to update all widgets from onEnabled
相关推荐
最新文章