activity 关闭后服务停止工作,activity 停止时调试器停止

Service stops working after activity closed and debugger stops when activity stops

我正在尝试调试我的应用程序的服务,但没有成功。启动器会打开一个 activity,可以从中停止和启动服务。该服务在这里没有问题(activity 处于打开状态)。当 activity 关闭时,服务应该保持 运行 但它没有。一旦我关闭 activity,调试器就会分离。我需要一种方法让调试器保持打开状态,以便我可以看到导致服务终止的错误。

这就是我在 activity 中开始服务的方式:

@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
    if (isChecked && !isServiceRunning()) {
        if (checkPermissions()) {
            startService(new Intent(this, MainService.class).setAction(MainService.ACTION_START));
        } else {
            requestPermissions();
            toggle.setChecked(isServiceRunning());
        }
    } else if (!isChecked && isServiceRunning()) {
        stopService(new Intent(this, MainService.class));
    }
}

private boolean isServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    if (manager != null) {
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)){
            if("com.package.MainService".equals(service.service.getClassName())) {
                return true;
            }
        }
    }
    return false;
}

这是我的大部分服务class:

public class MainService extends Service {

public static final String ACTION_START = "START";
public static final String ACTION_FOUND = "FOUND";
public static final String ACTION_RECEIVED = "RECEIVED";

int volume;
final int notificationID = 94729;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if (ACTION_RECEIVED.equals(intent.getAction())) {
        log("Received Message");

        Bundle extras = intent.getExtras();
        if (extras != null) {
            String msg = extras.getString("msg");
            String sender = extras.getString("sender");
            receivedMessage(msg, sender);
        } else {
            log("There was no message");
        }
    } else if(ACTION_FOUND.equals(intent.getAction())) {
        log("Received Found");
        found();
    } else if (ACTION_START.equals(intent.getAction())) {
        log("Starting MainService");

        setupNotification();

        Toast.makeText(this, "Service Running", Toast.LENGTH_LONG).show();
    } else {
        stopSelf();
    }

    return START_STICKY;
}

@Override
public void onDestroy() {
    log("Destroying MainService");
    super.onDestroy();
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}

编辑:

这是我的清单文件:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity" android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".MainService" />

    <receiver android:name=".BroadcastListener">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

</application>

非常感谢任何帮助。

从前台服务播放音乐,这将阻止它重新启动。

官方 Android 文档说

A music player that plays music from a service should be set to run in the foreground, because the user is explicitly aware of its operation. The notification in the status bar might indicate the current song and allow the user to launch an activity to interact with the music player.

这是一个example