通知按钮不适用于某些Android设备

时间:2015-07-09 18:40:34

标签: android button service notifications android-notifications

我有奇怪的问题。我用动作按钮实现了通知,这完全适用于Adroid 5.0,但不适用于Android 4.2或更高版本(whathever 4.0 android)。我可以看到按钮,我可以点击按钮,但没有任何反应。全局通知意图在两个版本上都运行良好,只有动作按钮不是:/有人可以告诉我它是如何可能的吗??

这是我的代码:

 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的问题就像点击通知按钮后一样,没有任何反应,也没有启动"意图" ??我在那里没有登录......:/在某些设备上工作,有些设备没有

0 个答案:

没有答案