安卓小部件点击和广播接收器不起作用

5
以下代码应该描述一个应用程序,其中一旦小部件按钮被点击,它会发送一个意图,该意图应该被TestReceiver接收。然而,在运行下面的代码时,TestReceiver的onReceive从未被调用。
请问有人可以告诉我我做错了什么吗?
小部件代码
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);
      }
     }

    }

清单文件:

    <?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> 

首先问一个愚蠢的问题 - 如果您只是在应用程序中创建一个常规的Intent()并调用startActivity(),它是否有效?只是确保接收器设置正确。 - EboMike
我在小部件的onUpdate函数中添加了context.sendBroadcast(intent);。现在正在调试它,似乎它正在调用该语句上的接收器,而不是在我点击按钮时。我认为我感到困惑是因为我正在进行的Toast调用没有任何作用。 - Kratz
是的,因为你没有添加 .show() :) - EboMike
啊,学习新东西的挣扎。噢,好吧,至少最终它能够工作了。 - Kratz
不在Toast的末尾添加show()是一个常见的错误。无论如何,很高兴它能够工作。嘿,我也可以添加一个答案来结束这个问题。 - EboMike
由于AppWidgetProvider扩展了BroadcastReceiver,您无需创建单独的接收器。只是说一下... - elgui
2个回答

9
很可能它是有效的,但你忘记在Toast结尾处添加.show()了 :)

0

== 用于测试引用相等性(即它们是否是同一个对象)。

.equals() 用于测试值相等性(即它们是否在逻辑上“相等”)。

字符串值使用 '==' 而不是 'equals' 进行比较

将 "if(intent.getAction()==TEST_INTENT)" 更改为 "if(intent.getAction().equals(TEST_INTENT))"

当然还有 Toast.makeText(context, "Test", Toast.LENGTH_SHORT).show();

所有代码:

package *********;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;


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 holaaa", Toast.LENGTH_SHORT).show();

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接