2013-02-18 39 views
0

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

在此先感谢

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 

回答

4

很简单。

你包的名字是package com.example.newtets;

和清单预计package com.example.newtest;

所以固定错字,因为你传递com.example.newtets.BootCompletedIntentReceiver类,而不是

+1

+1鹰眼抓:) – user2060383 2013-02-18 11:17:20

+0

谢谢谢谢......你救了我的一天......一个完美的视觉......一些小错误将浪费我们的时间......你们真的是夏普(不是鲨鱼) – Riskhan 2013-02-18 11:26:13

+0

不客气:) – Shark 2013-02-18 11:30:09