代码之家  ›  专栏  ›  技术社区  ›  TheChubbyPanda

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

  •  1
  • TheChubbyPanda  · 技术社区  · 7 年前

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

    以下是我在活动中开始服务的方式:

    @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 回复  |  直到 6 年前
        1
  •  1
  •   Alex Kamenkov    7 年前

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

    Android官方文档显示 :

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

    这是一个 example