Android 如何按前台服务通知?

Android 如何按前台服务通知?,android,service,onclick,foreground,Android,Service,Onclick,Foreground,我有一个前台服务通知,我希望当用户关闭应用程序并按下通知再次打开应用程序时 onclicklistener使用onClick()方法,但它没有执行任何操作。如果不可能,我希望在通知中显示“播放”按钮,因为它仅在我展开通知时显示 这是服务类: public class MyForeGroundService extends Service { public MyForeGroundService() { } @Override public IBinder onBind(Intent inte

我有一个前台服务通知,我希望当用户关闭应用程序并按下通知再次打开应用程序时 onclicklistener使用onClick()方法,但它没有执行任何操作。如果不可能,我希望在通知中显示“播放”按钮,因为它仅在我展开通知时显示

这是服务类:

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(PendingEvent)”方法


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

非常感谢!那么我就用第一个解决方案:P