Android部件点击和广播接收器不工作 [英] Android Widget Click and Broadcast Receiver Not Working

查看:97
本文介绍了Android部件点击和广播接收器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code应该描述一个应用程序,即一旦被点击的小部件按钮,它发送过应该由TestReceiver收到的意图。然而,在运行我的跌破code,TestReceiver的的onReceive没有被调用。

可能有人让我知道我做错了什么?

小工具code

 公共类的Widget扩展AppWidgetProvider {

公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
    最终诠释N = appWidgetIds.length;

    //执行属于此提供这一循环过程中的每个应用程序的widget
    的for(int i = 0; I&n种;我++){
        INT appWidgetId = appWidgetIds [I]

        //创建一个Intent推出为ExampleActivity
        //意向意图=新的意图(context.getApplicationContext(),TestReceiver.class);
        意向意图=新的意图();
        intent.setAction(TestReceiver.TEST_INTENT);
        intent.setClassName(TestReceiver.class.getPackage()的getName(),TestReceiver.class.getName());

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(),0,意向,PendingIntent.FLAG_CANCEL_CURRENT);

        //获取该应用程序窗口小部件的布局,并附加一点击监听器的按钮
        RemoteViews意见;

       意见=新RemoteViews(context.getPackageName(),R.layout.main);

        views.setOnClickPendingIntent(R.id.btnTest,pendingIntent);

        //告诉AppWidgetManager对当前应用程序窗口小部件进行更新
        appWidgetManager.updateAppWidget(appWidgetId,意见);



    }


}
 

}

接收器code:

 公共类TestReceiver扩展的BroadcastReceiver {

     公共静态最后弦乐TEST_INTENT =MyTestIntent;

     @覆盖
     公共无效的onReceive(上下文的背景下,意图意图){
      // TODO自动生成方法存根

      Toast.makeText(上下文中,测试,Toast.LENGTH_SHORT);

      如果(intent.getAction()== TEST_INTENT)
      {
         的System.out.println(GOT的意图);

       Toast.makeText(上下文中,测试,Toast.LENGTH_SHORT);
      }
     }

    }
 

清单:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      包=com.test.intenttest
      安卓版code =1
      机器人:VERSIONNAME =1.0>
    <应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME>
  <接收机器人:TestReceiver名称=机器人:标签=@字符串/ APP_NAME>
   <意向滤光器>
    <作用机器人:名称=MyTestIntent>
    < /作用>
   &所述; /意图滤光器>
  < /接收器>
  <接收机器人:标签=@字符串/ APP_NAME机器人:NAME =小工具>
   <意向滤光器>
    <作用机器人:名称=android.appwidget.action.APPWIDGET_UPDATE/>
   &所述; /意图滤光器>
   <元数据的android:NAME =android.appwidget.provider安卓资源=@ XML /小工具/>
  < /接收器>
    < /用途>
    <使用-SDK安卓的minSdkVersion =3/>

< /舱单>
 

解决方案

这可能是工作,但你忘了添加 .show()在你敬酒的结尾: )

The below code should describe an app where once the widget button is clicked, it sends off an intent that should be received by TestReceiver. However, in running my below code, the onReceive of TestReceiver is never called.

Could someone let me know what I'm doing wrong?

Widget code

public class Widget extends AppWidgetProvider {

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];

        // Create an Intent to launch ExampleActivity
        //Intent intent = new Intent(context.getApplicationContext(), TestReceiver.class);
        Intent intent = new Intent();
        intent.setAction(TestReceiver.TEST_INTENT);
        intent.setClassName(TestReceiver.class.getPackage().getName(), TestReceiver.class.getName());

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        // Get the layout for the App Widget and attach an on-click listener to the button
        RemoteViews views;

       views = new RemoteViews(context.getPackageName(), R.layout.main);     

        views.setOnClickPendingIntent(R.id.btnTest, pendingIntent);

        // Tell the AppWidgetManager to perform an update on the current App Widget
        appWidgetManager.updateAppWidget(appWidgetId, views);



    }


}

}

Receiver Code:

   public class TestReceiver extends BroadcastReceiver {

     public static final String TEST_INTENT= "MyTestIntent";

     @Override
     public void onReceive(Context context, Intent intent) {
      // TODO Auto-generated method stub

      Toast.makeText(context, "Test", Toast.LENGTH_SHORT);

      if(intent.getAction()==TEST_INTENT)
      {
         System.out.println("GOT THE INTENT");

       Toast.makeText(context, "Test", Toast.LENGTH_SHORT);
      }
     }

    }

Manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test.intenttest"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
  <receiver android:name=".TestReceiver" android:label="@string/app_name">
   <intent-filter>
    <action android:name="MyTestIntent">
    </action>
   </intent-filter>
  </receiver>
  <receiver android:label="@string/app_name" android:name="Widget">
   <intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
   </intent-filter>
   <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget"  />
  </receiver>
    </application>
    <uses-sdk android:minSdkVersion="3" />

</manifest> 

解决方案

It probably works, but you forgot to add .show() at the end of your Toast :)

这篇关于Android部件点击和广播接收器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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