Android小工具将不会有一个点击 [英] Android Widget won't have a click

查看:102
本文介绍了Android小工具将不会有一个点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我已经有了这个小工具,这是应该显示吐司一旦点击了。

Basically I've got this Widget, that's supposed to show a Toast once it is clicked.

public class WidgetActivity extends AppWidgetProvider {


 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
     for (int i = 0; i < appWidgetIds.length; i++) {  

         int appWidgetId = appWidgetIds[i];  
         Intent intent = new Intent(context, WidgetActivity.class);  
         intent.setAction("ActionOne");  
         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);  
         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
         views.setOnClickPendingIntent(R.id.LinLayWiget, pendingIntent);  
         appWidgetManager.updateAppWidget(appWidgetId, views);  

     }

 }



 @Override  
 public void onReceive(Context context, Intent intent) {  

     Log.e("YYY","YYYY");
     Toast.makeText(context, "AAA", 1500).show();

     if (intent.getAction().equals("ActionOne")) {  

         Log.e("X","X");
         Toast.makeText(context, "I'm CLICKED!", 1500).show();

        }  
     super.onReceive(context, intent);  
 } 




}

清单:

...
        <receiver android:name="WidgetActivity" android:label="FXMaster" android:icon="@drawable/assiconwi">

            <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widgetprovider" />
                <intent-filter>
                        <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
                </intent-filter>
        </receiver>
...

但是,一旦小部件被点击时,没有任何反应。任何想法可能是错误的?

But once the widget is clicked, nothing happens. Any ideas what might be wrong?

谢谢!

推荐答案

我在这里救你。

1)的onUpdate 覆盖应致电(在例如方法的开头)

1) onUpdate override should call (in the beginning of the method for example)

super.onUpdate(context, appWidgetManager, appWidgetIds);

2),因为该行的:

2) Because of this line:

intent.setAction("ActionOne");

您的意图可以忽略不计,因此,code不会掉入如果在的onReceive 。添加一个唯一的标识符就像是在这个岗位<一做href=\"http://stackoverflow.com/questions/3140072/android-keeps-caching-my-intents-extras-how-to-declare-a-pending-intent-that-ke\">Android不断缓存我的意图附加功能,如何声明挂起的意图,保持新鲜的演员?

Your intents can be ignored, so the code wonT fall into the if in the onReceive. Add a unique identifier like it is done in this post Android keeps caching my intents Extras, how to declare a pending intent that keeps fresh extras?

在最后一个音符,我D转换我的类名,因为它不是一个实际的活动。

On a last note, I d change my class name since it is not an Activity actually.

干杯。

这篇关于Android小工具将不会有一个点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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