我如何获得通知意图数据

时间:2018-11-02 16:45:22

标签: java android notifications

我想在某个事件中显示一个通知,这很好,我也正在进行我想要的活动,但是问题是意图数据为空,请在这里,这里是代码

    Intent resultIntent = new Intent(context, MovieDetailActivity.class);
        resultIntent.putExtra(Constants.MOVIE_ID, cursor.getString(
                cursor.getColumnIndexOrThrow(DatabaseHelper.ID)));
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntentWithParentStack(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_download)
                .setContentIntent(resultPendingIntent)
                .setContentTitle(cursor.getString(
                        cursor.getColumnIndexOrThrow(DatabaseHelper.NAME)))
                .setAutoCancel(true)
                .setContentText(cursor.getString(
                        cursor.getColumnIndexOrThrow(DatabaseHelper.REALEASE_DATE)));
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(context.NOTIFICATION_SERVICE);

        notificationManager.notify(1, builder.build()
        );

我在这一行中使用意图发送一些数据。

resultIntent.putExtra(Constants.MOVIE_ID, cursor.getString(
                cursor.getColumnIndexOrThrow(DatabaseHelper.ID)));

我在这里接收数据,但它为空。

Intent receivedIntent = getIntent();
    mMovieId = receivedIntent.getIntExtra(Constants.MOVIE_ID, -1);

请帮助,我花了整整一整天的时间。

2 个答案:

答案 0 :(得分:1)

您将Intent传递给String,这样就无法从Intent中获取Integer,这就是为什么您获得null的原因。

因此更改此行:

mMovieId = receivedIntent.getIntExtra(Constants.MOVIE_ID, -1);

 mMovieId = receivedIntent.getStringExtra(Constants.MOVIE_ID);  
// and simply parse string to integer  
       int id = Integer.parseInt(mMovieId);

答案 1 :(得分:0)

您需要更改您的待定意向:

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0 , resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

希望它能解决您的问题。