Loading a service after booting in android [closed

2019-08-24 13:28发布

I has taken a sample from internet for implementing service that starts automatically after phone boots. But application shows error. Please help me to solve problem

thanks in advance

xml file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newtest"
android:versionCode="1"
android:versionName="1.0">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>  
<application>  
    <receiver android:name="com.example.newtest.BootCompletedIntentReceiver"
                      android:permission="android.permission.RECEIVE_BOOT_COMPLETED">  
        <intent-filter>  
            <action android:name="android.intent.action.BOOT_COMPLETED" />  
        </intent-filter>  
    </receiver>  
    <service android:name="com.example.newtest.BackgroundService"/>  
</application>
</manifest>

Broadcast receiver class

package com.example.newtest;

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

public class BootCompletedIntentReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {  
           Intent pushIntent = new Intent(context, BackgroundService.class);
           context.startService(pushIntent);  
    }
}
}

service class

package com.example.newtest;

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

public class BackgroundService extends Service {

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

@Override
public void onCreate() {
    Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();
    super.onCreate();
}

}

logcat

02-18 16:32:49.949: E/AndroidRuntime(308): FATAL EXCEPTION: main
02-18 16:32:49.949: E/AndroidRuntime(308): java.lang.RuntimeException: Unable to instantiate receiver com.example.newtest.BootCompletedIntentReceiver: java.lang.ClassNotFoundException: com.example.newtest.BootCompletedIntentReceiver in loader dalvik.system.PathClassLoader[/data/app/com.example.newtest-1.apk]
02-18 16:32:49.949: E/AndroidRuntime(308):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1773)
02-18 16:32:49.949: E/AndroidRuntime(308):  at android.app.ActivityThread.access$2400(ActivityThread.java:117)
02-18 16:32:49.949: E/AndroidRuntime(308):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
02-18 16:32:49.949: E/AndroidRuntime(308):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 16:32:49.949: E/AndroidRuntime(308):  at android.os.Looper.loop(Looper.java:123)
02-18 16:32:49.949: E/AndroidRuntime(308):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 16:32:49.949: E/AndroidRuntime(308):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 16:32:49.949: E/AndroidRuntime(308):  at java.lang.reflect.Method.invoke(Method.java:507)
02-18 16:32:49.949: E/AndroidRuntime(308):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 16:32:49.949: E/AndroidRuntime(308):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 16:32:49.949: E/AndroidRuntime(308):  at dalvik.system.NativeStart.main(Native Method)
02-18 16:32:49.949: E/AndroidRuntime(308): Caused by: java.lang.ClassNotFoundException: com.example.newtest.BootCompletedIntentReceiver in loader dalvik.system.PathClassLoader[/data/app/com.example.newtest-1.apk]
02-18 16:32:49.949: E/AndroidRuntime(308):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-18 16:32:49.949: E/AndroidRuntime(308):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-18 16:32:49.949: E/AndroidRuntime(308):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-18 16:32:49.949: E/AndroidRuntime(308):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1764)
02-18 16:32:49.949: E/AndroidRuntime(308):  ... 10 more

1条回答
姐就是有狂的资本
2楼-- · 2019-08-24 14:14

It's simple.

Your package name is package com.example.newtets;

And manifest expects package com.example.newtest;

So fix the typo, since you're passing com.example.newtets.BootCompletedIntentReceiver class instead

查看更多
登录 后发表回答