我的pendingIntent没有为服务添加任何额外内容,尽管我在intent或pendingIntent本身中放置了标记。
DrawerActivity.class:
@Override
public void registerAlarmTask(Calendar time, String title, int iconId) {
int requestId = (int) System.currentTimeMillis();
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("title",title);
intent.putExtra("icon", iconId);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setAction("alarm");
PendingIntent pIntent = PendingIntent.getBroadcast(this, requestId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar newTime = Calendar.getInstance();
newTime.set(Calendar.MINUTE, time.get(Calendar.MINUTE)-1);
alarm.set(AlarmManager.RTC_WAKEUP, newTime.getTimeInMillis(), pIntent);
Log.w(TAG, "Alarm created! " + newTime.toString());
}
来自NotificationService的StartCommand方法:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);
Log.w(TAG, "Icon ID: " + intent.getIntExtra("icon", 404) + " / Title: " + intent.getStringExtra("title"));
nBuilder.setSmallIcon(intent.getIntExtra("icon", R.drawable.ic_event_new));
nBuilder.setContentTitle(intent.getStringExtra("title"));
nBuilder.setContentText("A partida irá começar em instantes...");
nBuilder.setTicker("Sua partida no Destiny está prestes a começar!");
nBuilder.setAutoCancel(true);
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(0, nBuilder.build());
Log.w(TAG, "Notification Service started!");
Toast.makeText(this, "Notification Service started!", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
无论requestId,intent flags还是pendingIntent标志,意图的额外内容始终为null。
我在哪里做错了?我经常搜索,但没有找到答案。
此致 Lucas Sene
答案 0 :(得分:0)
就像Mike M.在评论中所说,我只是忘了将Intent额外从BroadcastReceiver传递给服务。是的,这是一个愚蠢的错误......
感谢所有评论!