AppWidget按钮的onClick停止工作 [英] AppWidget Button onClick stops working

查看:304
本文介绍了AppWidget按钮的onClick停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小窗口,我试图建立一个只包含一个按钮。什么我想要做的是有按钮按下,然后运行一些简单的一张code的(在我的测试是敬酒警报)。它似乎做工精细开始,却突然按钮停止响应点击。我一直觉得,注意到它的手机已经睡着了。这里是我的code的AppWidgetProvider。

I have a widget I'm trying to build that consists of only a button. What I'm wanting to do is have the button clicked and then run some simple piece of code (in my test it's a toast alert). It seems to work fine initially, but suddenly the button stops responding to clicks. I've noticed it consistently after the phone has been asleep. Here is my code for the AppWidgetProvider.

的onUpdate:

for (int appWidgetId : appWidgetIds) {
    RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.my_widget);
    Intent intent = new Intent(context, MyNewWidgetProvider.class);
    intent.setAction("MyCode");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    remoteView.setOnClickPendingIntent(R.id.my_btn, pendingIntent);

    AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteView);
}

的onReceive:

onReceive:

super.onReceive(context, intent);

if (intent.getAction().equals("MyCode")) {          
    Toast toast = Toast.makeText(context, "It worked", Toast.LENGTH_SHORT);
    toast.show();   

}

我相当为难,所以如果有人能帮点我朝着正确的方向我想AP preciate它。就像我说的,它工作正常,直到手机是睡着一两分钟,然后将其完全停止响应的点击。

I'm rather stumped, so if anyone can help point me in the right direction I would appreciate it. Like I said, it works fine until the phone is asleep for a minute or two, then it completely stops responding to clicks.

感谢您!

推荐答案

挂起的意图每次使用后烧。您需要重新设置。或等待部件得到刷新,然后就发生了,但是这可能不是所期望的方式。

The pending intent is "burned" after each use. You need to set it again. Or wait for the widget to get refreshed, then it happens, too, but that's probably not the desired way.

这篇关于AppWidget按钮的onClick停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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