android - PendingIntent 和 Notification Intent

标签 android android-intent android-pendingintent

我发现下面的代码是多余的。我在这里错过了一些基本的东西吗?有没有办法减少这里的重复代码。我可以使用 Intent 或 PendingIntent 对象吗?为什么要同时使用?

    Intent updateUI = new Intent(SENDTOBACKGROUND_SERVICE);
    updateUI.putExtra("Signal", God.YELLOW);
    sendBroadcast(updateUI);

    Intent sendNotification = new Intent(DriverService.this, DriverHome.class);
    sendNotification.putExtra("Signal", God.YELLOW);

    PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, sendNotification, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification n = new NotificationCompat.Builder(DriverService.this)
            .setContentTitle("Attempting to update location")
            .setContentText("Cab @(last) " + currentLocationInText)
            .setSmallIcon(R.drawable.yellow).setContentIntent(pIntent).setAutoCancel(true)
            .build();

    ((NotificationManager) DriverService.this.getSystemService(NOTIFICATION_SERVICE)).notify("Taxeeta", R.id.cabLocation, n);

最佳答案

不,你需要两者。 PendingIntentIntent 的包装器。如果没有要包装的 Intent,就不能有 PendingIntent。并且您不能将 Intent 放入 Notification 中。当你想将一个Intent交给另一个组件时,你需要使用一个PendingIntent,以便另一个组件可以为你发送Intent (作为一种“代理”)在未来的某个时候。

关于android - PendingIntent 和 Notification Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19384002/

相关文章:

android - 从现有任务中的通知开始新 Activity

android - 无法扩充 android.view.inflateException.Binary xml 文件行 #2

android - 将 intent extras 从谷歌云消息接收器传递到 Activity 是空的

android - 如何停止 AlarmManager

android - 使用 cancel() 从 AlarmManager 删除警报 - Android

安卓 Intent : Start activity using class name from another app that has the same sharedUserId

java - 如何在 Android 中使用 java 在点击时放大/缩小 imageView?

android - 在可组合函数中调用另一个类的方法

Java IncationHandler 和 Singleton

java - 如何在聊天中显示超链接以打开浏览器 Intent