为什么小部件不重新启动后工作? [英] Why widget does not work after reboot?

查看:181
本文介绍了为什么小部件不重新启动后工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的小工具,当我点击它,它应该打开我的活动,它的工作原理,但它不会重新启动后正常工作。我必须删除,然后重新添加小部件到我的主屏幕,因为当我点击小窗口小部件不响应,不打开我的活动。那么问题出在哪里?

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?

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

这篇关于为什么小部件不重新启动后工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆