【问题标题】:Foreground Service doesn't run in Oreo前台服务不在 Oreo 中运行
【发布时间】:2018-11-21 09:34:56
【问题描述】:

我有一个在我的手机(Android 7.0)上运行的前台服务,但它不能在朋友的手机(Android 8.0)上运行。我也在 android studio 模拟器上对其进行了测试,我在 8.0 版上也遇到了同样的问题。我已经实现了 if-else 检查 以查看它是否是 Android 8.0,所以我可以调用 startForegroundService() 并且仍然没有。

我是这样称呼它的:

 //Start foreground service
    Intent intent = new Intent(this, MyForeGroundService.class);
    intent.setAction(MyForeGroundService.ACTION_START_FOREGROUND_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(intent);
    }else{
        startService(intent);
    }

这是我的前台服务:

public class MyForeGroundService extends Service implements LocationListener {

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();
    wakeLock();
}

@SuppressLint("InvalidWakeLockTag")
public void wakeLock() {
    pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    assert pm != null;
    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "partialwl");
    wl.acquire();
}

public  void stopWakeLock() {
    wl.release();
}

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

        switch (action) {
            case ACTION_START_FOREGROUND_SERVICE:
                wakeLock();
                startForegroundService();
                break;
            case ACTION_STOP_FOREGROUND_SERVICE:
                stopWakeLock();
                stopForegroundService();
                break;
            case ACTION_PLAY:
                Intent openMap= new Intent(this,Map.class);
                startActivity(openMap);
                break;
            case ACTION_PAUSE:
                break;
        }
    }
    return START_STICKY;
}

/* 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(this,Map.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

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

    //Go back to Map activity if user press at the notification
    builder.setContentIntent(pendingIntent)
            .setContentTitle("gEKOning...")
            .setContentText("Tap to open gEKOn app");


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


    // 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();
}

【问题讨论】:

  • 这个答案应该对你有帮助:stackoverflow.com/a/53081689/4255978
  • @HedeH 这个频道名称是什么?我在哪里知道名称是什么?
  • 没关系,只是Android管理通知。如果您想了解更多信息,可以阅读通知渠道:developer.android.com/training/notify-user/channels
  • @HedeH 非常感谢!工作正常!再问一个问题。我想实现我自己的标题、文本和图像,当用户按下通知打开应用程序时我也想要。现在,如果用户按下应用程序,它会打开设置。有什么想法吗?
  • 它会打开设置,因为您没有指定PendingIntenttitle 等。所以系统正在为您创建通知。您需要向Notification.Builder 添加内容。看这里:developer.android.com/training/notify-user/build-notification

标签: android android-8.0-oreo foreground-service


【解决方案1】:

您需要定义一个通知渠道。这样的事情就可以了。

private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = getString(R.string.channel_name);
    String description = getString(R.string.channel_description);
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
    channel.setDescription(description);
    // Register the channel with the system; you can't change the importance
    // or other notification behaviors after this
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}}

【讨论】:

  • 包括我在内的一些人在模拟器中测试通知时会感到困惑,即 FCM 无法正常工作。我在 Nexus 5 虚拟设备中进行测试。我没有意识到它是在 Android O 上运行的。好电话
猜你喜欢
  • 2019-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多