在android中启动后加载服务

时间:2013-02-18 11:13:02

标签: android android-service

我从互联网上取样来实现在手机启动后自动启动的服务。但应用程序显示错误。请帮我解决问题

提前致谢

xml文件

<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>

广播接收器类

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

服务类

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 个答案:

答案 0 :(得分:4)

很简单。

您的包名称为package com.example.newtets;

并且清单需要package com.example.newtest;

所以修正错字,因为你正在传递com.example.newtets.BootCompletedIntentReceiver