我发现下面的代码是多余的。我在这里错过了一些基本的东西吗?有没有办法减少这里的重复代码。我可以使用 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);
最佳答案
不,你需要两者。 PendingIntent
是 Intent
的包装器。如果没有要包装的 Intent
,就不能有 PendingIntent
。并且您不能将 Intent
放入 Notification
中。当你想将一个Intent
交给另一个组件时,你需要使用一个PendingIntent
,以便另一个组件可以为你发送Intent
(作为一种“代理”)在未来的某个时候。
关于android - PendingIntent 和 Notification Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19384002/