简体   繁体   English

BOOT_COMPLETED后,服务被迫停止

[英]Service forced stopped after BOOT_COMPLETED

I'm developing a Service that runs in the background and listens to notifications from a server. 我正在开发一种在后台运行并侦听来自服务器的通知的服务。 This Service has to start when the device boots. 设备启动时必须启动此服务。 Somehow, I can not get it to work. 不知何故,我无法正常工作。 The Service starts, but then it's stopped by the system. 服务已启动,但随后被系统停止。

Even following the schema proposed in https://stackoverflow.com/a/7690600/981393 , after the Service is started I see that it's being force stopped by the system. 即使遵循https://stackoverflow.com/a/7690600/981393中提出的模式,服务启动后,我也看到它被系统强行停止

So I'm wondering, is this the way is supposed to be? 所以我想知道,这应该是这样吗?

Any help would be appreciated. 任何帮助,将不胜感激。

Receiver 接收者

public class SSBankBootReceiver extends BroadcastReceiver {

private static final String TAG = "SSBankBootReceiver";
private static final long PERIOD = 60000 * 1;

@Override
public void onReceive(Context ctxt, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        Log.d(TAG, "Boot completed.");

        startTestService(ctxt);
    }
}

private void startTestService(Context ctxt) {
    Log.d(TAG, "Starting test service...");
    Intent i = new Intent(ctxt, TestService.class);
    ctxt.startService(i);
}

Service 服务

public class TestService extends Service {

private static final String TAG = "TestService";

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

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

    super.onStartCommand(intent, flags, startId);
    return START_REDELIVER_INTENT;
}

private void printMessages() {
    // TODO Auto-generated method stub
    while ( true) {
        try {
            Log.d(TAG, "Imprimiendo nuevo mensaje...");
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

}

Manifest 表现

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".DashboardActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <receiver android:name="<package>.service.SSBankBootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

    <service android:name="<package>.service.TestService"></service>

</application>

Log 日志记录

11-04 19:38:04.916: I/ActivityManager(276):   Force stopping service ServiceRecord{41135258 u0 <package>/.service.TestService}

This is how i fix this exact same problem : 这就是我解决这个完全相同的问题的方法:

    //do things you want
    super.onStartCommand(intent, flags, startId);
    return START_REDELIVER_INTENT; //this is the ANSWER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM