2

所以我改变了我的代码来调用广播接收器而不是调用主要活动

Intent notificationIntent = new Intent(context, com.plugin.statusNotificationForGCM.statusNotificationForGCMReceiver.class);

notificationIntent.putExtra(NOTIF_RESPOND, runThis);
notificationIntent.setAction(Intent.ACTION_VIEW);
notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

//contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
contentIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

问题是我在我的接收器中添加了这个,但不认为它正在运行。单击通知时看不到标签并登录控制台

public class statusNotificationForGCMReceiver extends BroadcastReceiver {
    private static final String TAG = "statusNOTIFICATIONReceiver";

    @Override
    public final void onReceive(Context context, Intent intent) {
        Log.v(TAG,"ASDFSJD FASDF ASDPFJA SDFPAUS DFPAS DFASDF");
    }
}

有什么我想念的吗?我必须在清单文件中添加一些东西吗?

谢谢

4

1 回答 1

5

您可能需要在清单中有这个:

合适的意图在其定义<intent-filter>

<activity android:name="BroadcastIntents" android:label="@string/app_name"/>
    <receiver android:name="statusNotificationForGCMReceiver" android:label="@string/app_name">
       <intent-filter>
          <action android:name="android.test.BROADCAST" />
       </intent-filter>
    </receiver>

附加物:

一个意图过滤器让你告诉一个活动它可以/不能响应什么(当被意图调用时)。

public class InternalMessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context ctx, Intent intent) {
        String msg = intent.getStringExtra("message");
        Toast.makeText(ctx,msg,Toast.LENGTH_SHORT).show();
    }
}

您可以在 Activity 中调用它,例如单击这样的按钮:

private static final String ACTION = "android.test.BROADCAST";

public void onClick(View v) {
        /* Broadcast intent */
        Intent intent = new Intent(ACTION);
        intent.putExtra("message", msg);
        main_activity.sendBroadcast(intent);
}
于 2012-09-03T23:12:25.857 回答