有时从小部件启动活动会失败

发布于 2024-10-16 04:25:37 字数 1006 浏览 7 评论 0原文

我已经实现了一个应用程序小部件来在单击时启动我的活动。

WidgetProvideronUpdate() 方法:

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

     }
}

在主屏幕上添加小部件后,它会按预期工作。

但有时之后就无法再启动活动了!我必须删除该小部件并再次添加。

我该如何修复它?请帮忙。

I have implemented a App Widget to launch my activity when clicked.

onUpdate() method of WidgetProvider:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜警司 2024-10-23 04:25:37

我会这样做:

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

我不确定的一件事是对 super.onUpdate() 的调用。我自己的小部件代码没有它,并且似乎工作正常......不确定是否需要它。

我不知道这个重构是否能解决你的问题!

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(). 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!

拥醉 2024-10-23 04:25:37

我知道这已经晚了两年了,但我也一直在努力解决这个问题,直到今天我想我知道我做错了什么。我认为主要的关键是关注RemoteViews类的使用。您将这些对象准备为一种指令集,供另一个流程遵循。设置“点击挂起意图”必须在将其发送到 updateAppWidget 方法之前完成,因此您第一次调用该方法不会为您的“myView”对象提供点击准备。接下来,您的代码设置 onClick 触发器并再次调用 updateAppWidget。看起来应该可以工作,但是当两个意图不同或不明确时,有一个令人困惑的主题,您可能需要阅读该主题以了解为什么您的代码无法预测地工作。如果我是对的,那么要点就是不要第一次调用 updateAppWidget,然后始终确保在创建 RemoteViews 对象时设置 onClick 触发器。无论如何我希望如此。

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. 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. Your code next sets the onClick trigger and calls updateAppWidget a second time. 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. I hope so anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文