我有一个前台服务通知,我希望在用户关闭应用程序并按通知再次打开应用程序时实现。 使用onClick()方法的onclicklistener,但是它什么也没做。如果不可能的话,我希望在通知中显示“播放”按钮,因为只有在展开通知时才会显示该按钮。
以下是服务类别:
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();
}
}
答案 0 :(得分:2)
因此,要向通知中添加“ onClick”效果,应在NotificationCompat.Builder中使用“ .setContentIntent(pendingIntent)”方法。
根据我的阅读,要使播放按钮始终可见,有人说“ setPriority(Notification.PRIORITY_MAX)+ setWhen(0)”可以解决这个问题,而其他人则说这取决于设备和如果您还有其他通知,是否已连接到USB等,并且没有针对此的单个解决方案。