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

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

问题描述

我有这个简单的小部件,当我点击它时,它应该会打开我的活动,它可以工作,但重启后它就不能工作了.我必须删除然后再次将小部件添加到我的主屏幕,因为当我单击小部件时,小部件没有响应并且没有打开我的活动.那么问题出在哪里?

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?

代码:

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));
    }

推荐答案

重启后你会调用 OnEnabled 而不是 onUpdate 所以将代码从 onUpdate 移到一个单独的方法中,然后调用该方法从 onEnabled 更新所有小部件

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天全站免登陆