【问题标题】:Cannot keep Service running in Android无法让服务在 Android 中运行
【发布时间】:2018-10-07 04:05:28
【问题描述】:

我尝试了线程:

Cannot keep android service alive after app is closed

Android Service Stops When App Is Closed

我试过了,没有成功

即使应用程序被终止,我也希望 IntentService 在后台运行。但是,如果我从最近的屏幕中删除我的应用程序,我的服务就会停止。我怎样才能避免这种情况?换句话说,即使我的应用已从最近的应用中关闭,我如何才能让我的服务继续运行?

我的前台服务:

public class ForegroundService extends Service {
private static final String LOG_TAG = "ForegroundService";
public static boolean IS_SERVICE_RUNNING = false;

private NotificationManagerCompat notificationManagerCompat;
final NotificationCompat.Builder playingChannelID = new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_ID);


@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    notificationManagerCompat = NotificationManagerCompat.from(this);

    if (intent != null && intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
        showNotification();
    } else if (intent != null && intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {
        MainActivity.exoPlayer.setPlayWhenReady(false);
        notificationManagerCompat.cancel(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE);
        stopSelf();
    }
    return START_STICKY;
}
private void showNotification() {
    createNotificationChannel();

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
    Intent playIntent = new Intent(this, ForegroundService.class);
    playIntent.setAction(Constants.ACTION.STOPFOREGROUND_ACTION);
    PendingIntent pplayIntent = PendingIntent.getService(this, 0,
            playIntent, 0);
    Bitmap icon = BitmapFactory.decodeResource(getResources(),
            R.drawable.r);

    playingChannelID.setContentTitle("Service");
    playingChannelID.setContentText("Playing...");
    playingChannelID.setSmallIcon(R.drawable.Service);
    playingChannelID.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false));
    playingChannelID.setContentIntent(pendingIntent);
    playingChannelID.setOngoing(true);
    playingChannelID.addAction(android.R.drawable.ic_delete, "Turn Off",
                    pplayIntent).build();notificationManagerCompat.notify(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, playingChannelID.build());
}
@Override
public void onDestroy() {
    super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
    // Used only in case if services are bound (Bound Services).
    return null;
}

private void createNotificationChannel() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = Constants.NOTIFICATION_NAME;
        String description = Constants.NOTIFICATION_DESCRIPTION;
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel channel = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID, name, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
}

我的常数:

public class Constants {
public static String NOTIFICATION_CHANNEL_ID = "NOTIFICATION_CHANNEL_ID_RADIO";
public static String NOTIFICATION_NAME = "RADIO_NOTIFICATION";
public static String NOTIFICATION_DESCRIPTION = "NOTIFICATION_DESCRIPTION";
public interface ACTION {
    public static String MAIN_ACTION = "com.marothiatechs.foregroundservice.action.main";
    public static String PLAY_ACTION = "com.marothiatechs.foregroundservice.action.play";
    public static String STARTFOREGROUND_ACTION = "com.marothiatechs.foregroundservice.action.startforeground";
    public static String STOPFOREGROUND_ACTION = "com.marothiatechs.foregroundservice.action.stopforeground";
}
public interface NOTIFICATION_ID {
    public static int FOREGROUND_SERVICE = 101;
}
}

我的清单:

        <service android:name=".service.ForegroundService" >
        <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </service>

我看到应用程序打开时服务正在运行。当我通过主页按钮最小化应用程序时,它仍在运行。当我通过后退按钮关闭应用程序时,它仍在运行。但是如果我如上所述杀死它,它将停止。我该如何解决?

【问题讨论】:

    标签: android service android-service foreground-service


    【解决方案1】:

    如果你需要前台服务,你应该调用startForeground()方法。 我建议您阅读此article,如果您不想在 Oreo 设备及更高版本上使用 ANR。如果您在小米设备上测试您的应用,您需要在设置中允许您的应用自动启动。

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多