简体   繁体   中英

Service not starting up after boot completed

I programmed a boot up receiver to start a service, but the service is not starting. I don't even think the broad cast receiver is receiving any intents.

I set the permission

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

I declared the receiver in the manifest

<receiver android:name=".ServiceStarter">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

Here is my broad cast receiver class. I'm not logging any output in this class after boot up.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class ServiceStarter extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent("com.prac.test.MyPersistingService");
        i.setClass(context, MyPersistingService.class);
        context.startService(i);

        Log.v("TAG","Broadcast received"); //Doesn't print anything
    }
}

Here is my service class. None of the toasts are displayed after boot up.

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyPersistingService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();         //Doesn't show up
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();          //Doesn't show up
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show(); //Doesn't show up
        Log.v("TAG", "Service started");
    }
}

Try this:

<receiver android:name=".ServiceStarter" android:enabled="true" >
    <intent-filter>
       <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

if still dosn't work please try this

 <receiver
    android:name=".ServiceStarter"
    android:enabled="true"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
     <intent-filter>
       <action android:name="android.intent.action.BOOT_COMPLETED" />    
       <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
 </receiver>

Good luck

It's very likely that your service is starting up, but is being killed off before it can do anything meaningful. Try acquiring a partial wake lock - Mark Murphy has written a handy set of classes just for that purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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