简体   繁体   English

有时有时无法从小部件启动活动

[英]Launching activity from widget fail after sometimes

I have implemented a App Widget to launch my activity when clicked. 我实现了一个应用小部件,以在单击时启动我的活动。

onUpdate() method of WidgetProvider : WidgetProvider onUpdate()方法:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    final int N = appWidgetIds.length;
    for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];

        RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.mywidgetprovider_layout);
        // ....update updateViews here
        appWidgetManager.updateAppWidget(appWidgetId, updateViews);

        Intent onClickedIntent = new Intent(context,MyActivity.class);
        PendingIntent pi = PendingIntent.getActivity(context, 0, onClickedIntent, 0);
        updateViews.setOnClickPendingIntent(R.id.myView, pi);

        appWidgetManager.updateAppWidget(appWidgetId, updateViews);

     }
}

It work as expected after the widget added on home screen. 在主屏幕上添加小部件后,它可以按预期工作。

But after sometimes, it cannot launch the activity again! 但是有时之后,它将无法再次启动活动! I have to remove the widget and add again. 我必须删除小部件并再次添加。

How can I fix it? 我该如何解决? please help. 请帮忙。

I know this is like two years late but I struggled with this too until today when I think I know what I was doing wrong. 我知道这已经晚了两年,但是直到今天,当我认为我知道自己做错了什么时,我也为此付出了努力。 I think the main key is to focus on the use of the RemoteViews class. 我认为主要的关键是专注于RemoteViews类的使用。 You prepare these objects as a sort of instruction set for a another process to follow. 您将这些对象作为一种指令集进行准备,以供其他过程遵循。 Setting the "on click pending intent" must done before sending it to the updateAppWidget method, so your first call to that method won't prime your "myView" object for clicks. 必须设置“单击时待处理的意图”, 然后才能将其发送到updateAppWidget方法,因此,对该方法的首次调用不会使您的“ myView”对象引发点击。 Your code next sets the onClick trigger and calls updateAppWidget a second time. 接下来,您的代码将设置onClick触发器,并再次调用updateAppWidget。 It looks like that one should work but there is a whole confusing subject regarding just when two intents are distinct or ambiguous which you may want to read about to understand why your code is working unpredictably. 看起来一个人应该工作,但是关于两个意图何时不同或模棱两可的问题,您可能要阅读一下,以了解为什么您的代码无法正常工作,这使整个主题令人困惑。 If I'm right, the take-away is to simply not call updateAppWidget the first time and then always make sure to set your onClick trigger whenever creating RemoteViews objects. 如果我是对的,最重要的是不要第一次调用updateAppWidget,然后始终确保在创建RemoteViews对象时设置onClick触发器。 I hope so anyway. 我还是希望如此。

I'd do it like this: 我会这样:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.mywidgetprovider_layout);    
    Intent onClickedIntent = new Intent(context,MyActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 0, onClickedIntent, 0);
    updateViews.setOnClickPendingIntent(R.id.myView, pi);

    for (int i=0; i<appWidgetIds.length; i++) {
        appWidgetManager.updateAppWidget(appWidgetIds[i], updateViews);
     }
}

One thing I'm not sure on is the call to super.onUpdate() . 我不确定的一件事是对super.onUpdate()的调用。 My own widget code doesn't have it and seems to work fine... not sure if it's needed or not. 我自己的窗口小部件代码没有它,并且看起来工作正常……不确定是否需要它。

I don't know if this refactor will fix your issue though! 我不知道这种重构是否可以解决您的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM