这是我的代码:
public int onStartCommand(Intent intent, int flags, int startId) {
switch (intent.getAction()) {
case ServiceUtils.ACTION_SERVICE_START_FOREGROUND:
...
if (object != null) {
buildNotification();
...
}
break;
case ServiceUtils.ACTION_SERVICE_PAUSE_FOREGROUND:
...
if (object != null) {
...
buildNotification();
}
break;
case ServiceUtils.ACTION_SERVICE_STOP_FOREGROUND:
// stop service
...
stopForeground(true);
stopSelf();
// dismiss notification from bar
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(...);
break;
}
return START_STICKY;
}
private void buildNotification() {
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setContentTitle(...)
.setTicker(getString(R.string.service_begin))
.setContentText(...)
.setSmallIcon(R.drawable.ic_menu_join)
.setLargeIcon(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_menu_join), 128, 128, false))
.setContentIntent(getNotificationIntent())
.setOngoing(true)
.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.service_notification_stop), getStopIntent());
if (condition) {
notification.addAction(android.R.drawable.ic_media_pause, getString(R.string.service_notification_pause), getPauseIntent());
}
startForeground(..._FOREGROUND_SERVICE, notification.build());
}
// GET INTENTS
private PendingIntent getStopIntent() {
Intent stopIntent = new Intent(this, ForegroundService.class);
stopIntent.setAction(ServiceUtils.ACTION_SERVICE_STOP_FOREGROUND);
stopIntent.putExtra(..., ...);
return PendingIntent.getService(this, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private PendingIntent getPauseIntent() {
Intent pauseIntent = new Intent(this, ForegroundService.class);
pauseIntent.setAction(ServiceUtils.ACTION_SERVICE_PAUSE_FOREGROUND);
pauseIntent.putExtra(..., ...);
pauseIntent.putExtra(..., ...);
return PendingIntent.getService(this, 0, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private PendingIntent getNotificationIntent() {
Intent notificationIntent;
if (condition) {
notificationIntent = new Intent(this, ...class);
} else {
notificationIntent = new Intent(this, ...class);
}
notificationIntent.putExtra(..., ...);
return PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
Android 4.0的问题就像点击通知按钮后一样,没有任何反应,也没有启动"意图" ??我在那里没有登录......:/在某些设备上工作,有些设备没有