如何按下前台服务通知?

How to press ON a foreground service Notification?

我有一个前台服务通知,我希望当用户关闭应用程序并按下通知以打开应用程序时 again.I 实现了一个 onclicklistener with onClick() 方法,但它确实 nothing.If 这是不可能的 我希望 "Play" 按钮显示在通知中,因为它只在我展开通知时显示。

这是服务class:

public class MyForeGroundService extends Service {

public MyForeGroundService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG_FOREGROUND_SERVICE, "My foreground service onCreate().");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        String action = intent.getAction();

        switch (action) {
            case ACTION_START_FOREGROUND_SERVICE:
                startForegroundService();
                Toast.makeText(getApplicationContext(), "Foreground service is started.", Toast.LENGTH_LONG).show();
                break;
            case ACTION_STOP_FOREGROUND_SERVICE:
                stopForegroundService();
                Toast.makeText(getApplicationContext(), "Foreground service is stopped.", Toast.LENGTH_LONG).show();
                break;
            case ACTION_PLAY:
                Toast.makeText(getApplicationContext(), "You click Play button.", Toast.LENGTH_LONG).show();
                break;
            case ACTION_PAUSE:
                Toast.makeText(getApplicationContext(), "You click Pause button.", Toast.LENGTH_LONG).show();
                break;
        }
    }
    return super.onStartCommand(intent, flags, startId);
}

/* Used to build and start foreground service. */
private void startForegroundService() {
    Log.d(TAG_FOREGROUND_SERVICE, "Start foreground service.");

    // Create notification default intent.
    Intent intent = new Intent();
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    // Create notification builder.
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Make notification show big text.
    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle.setBigContentTitle("Music player implemented by foreground service.");
    bigTextStyle.bigText("Android foreground service is a android service which can run in foreground always, it can be controlled by user via notification.");
    // Set big text style.
    builder.setStyle(bigTextStyle);

    builder.setWhen(System.currentTimeMillis());
    builder.setSmallIcon(R.mipmap.ic_launcher);
    Bitmap largeIconBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.strava);
    builder.setLargeIcon(largeIconBitmap);
    // Make the notification max priority.
    builder.setPriority(Notification.PRIORITY_MAX);
    // Make head-up notification.
    builder.setFullScreenIntent(pendingIntent, true);

    // Add Play button intent in notification.
    Intent playIntent = new Intent(this, MyForeGroundService.class);
    playIntent.setAction(ACTION_PLAY);
    PendingIntent pendingPlayIntent = PendingIntent.getService(this, 0, playIntent, 0);
    NotificationCompat.Action playAction = new NotificationCompat.Action(android.R.drawable.ic_media_play, "Play", pendingPlayIntent);
    builder.addAction(playAction);

    // Build the notification.
    Notification notification = builder.build();

    // Start foreground service.
    startForeground(1, notification);
}

private void stopForegroundService() {
    Log.d(TAG_FOREGROUND_SERVICE, "Stop foreground service.");

    // Stop foreground service and remove the notification.
    stopForeground(true);

    // Stop the foreground service.
    stopSelf();
}

}

因此,要向通知添加 "onClick" 效果,您应该使用 NotificationCompat.Builder 中的“.setContentIntent(pendingIntent)”方法。

根据我的阅读,让播放按钮始终可见有点棘手,有人说 "setPriority(Notification.PRIORITY_MAX) + setWhen(0)" 可以解决这个问题,其他人说这取决于设备,如果您有其他通知,您是否连接到 USB 等,并且没有单一的工作解决方案。