无法让服务在Android中运行

时间:2018-10-07 04:05:29

标签: android service android-service foreground-service

我尝试了线程:

Cannot keep android service alive after app is closed

Android Service Stops When App Is Closed

我尝试没有成功

即使应用程序被杀死,我也希望IntentService在后台运行。但是,如果我从“最近”屏幕中删除我的应用程序,则我的服务将停止。如何避免这种情况?换句话说,即使我的应用程序已从最近的应用程序中关闭,如何保持我的服务运行?

我的ForegroundService:

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>

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

1 个答案:

答案 0 :(得分:0)

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