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

时间:2018-01-12 20:49:52

标签: android debugging android-service android-debug

我正试图调试我的应用程序的服务,没有运气。启动器打开一个活动,可以从中停止和启动服务。这里的服务没有问题(活动开放时)。当活动关闭时,服务应该继续运行,但事实并非如此。一旦我关闭活动,调试器就会分离。我需要一种方法来保持调试器处于打开状态,以便我可以看到导致服务死亡的错误。

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

@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;
}

这是我的大多数服务类:

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>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

播放前台服务中的音乐,这样可以防止重启。

官方Android文档说

  

从服务播放音乐的音乐播放器应设置为在前台运行,因为用户明确知道其操作。状态栏中的通知可能指示当前歌曲,并允许用户启动活动以与音乐播放器进行交互。

这是example