BroadcastReceiver上的数据为空

时间:2016-07-27 19:47:32

标签: android android-intent broadcastreceiver

我试图通过Intent将数据发送到我的广播,然后由AndroidManifest录制。当我点击我的通知来触发广播时,我在方法OnReceive(Context context, Intent intent)

中得到null
Intent intentOnclick = new Intent(Intent.ACTION_SYNC, null, context, NotificationButton.class);
PendingIntent onClickPending = PendingIntent.getBroadcast(context, 0, intentOnclick, 0);

notificationView = new RemoteViews(context.getPackageName(), R.layout.download_notification);
intentOnclick.putExtra("test", notificationView);
intentOnclick.putExtra("test1", 1);

builder = new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.ic_launcher)
    .setAutoCancel(true)
    .setContent(notificationView);

notificationView.setProgressBar(R.id.download_progress, 100, 0, true);
notificationView.setTextColor(R.id.title_notification, Color.BLACK);
notificationView.setOnClickPendingIntent(R.id.play_or_pause_btn, onClickPending);

我的BroadcastReceiver:

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

        RemoteViews remoteViews = intent.getParcelableExtra("test");
        int test = intent.getIntExtra("test1", 265);
        // return null twice object
}

1 个答案:

答案 0 :(得分:1)

您必须将Intent 之前的额外内容添加到PendingIntent.getBroadcast()之前,否则它们将不会出现在实际发送的Intent