Androidの起動時にサービスを開始しようとすると問題が発生します。電話が起動するとすぐにアプリでエラーが発生し、停止する必要があります。ここにあります:
私のマニフェストの一部:
<service
android:name=".EventsNotificationService"
android:label="EventsNotificationService">
<intent-filter>
<action
android:name="it.bloomp.service.EventsNotificationService" />
</intent-filter>
/service>
<receiver
android:name=".receiver.StartEventsNotificationService"
android:enabled="true"
android:exported="true"
android:label="StartEventsNotificationService">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
私のサービス:
package it.bloomp.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class EventsNotificationService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service created", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this,"Service destroyed", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
super.onCreate();
Toast.makeText(this,"Service started", Toast.LENGTH_LONG).show();
}
}
私のサービススターター:
package it.bloomp.service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartEventsNotificationService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceIntent = new Intent("it.bloomp.service.EventsNotificationService");
context.startService(serviceIntent);
}
}
}
また、これにより起動時にサービスが開始されますが、常に実行するにはどうすればよいですか?
編集:
例外:
06-01 16:58:55.716: E/AndroidRuntime(483): 致命的な例外: メイン 06-01 16:58:55.716: E/AndroidRuntime(483): java.lang.RuntimeException: レシーバー it.bloomp をインスタンス化できません。 activity.receiver.StartEventsNotificationService: java.lang.ClassNotFoundException: it.bloomp.activity.receiver.StartEventsNotificationService 06-01 16:58:55.716: E/AndroidRuntime(483): android.app.ActivityThread.handleReceiver(ActivityThread.java: 2100) 06-01 16:58:55.716: E/AndroidRuntime(483): android.app.ActivityThread.access$1500(ActivityThread.java:123) 06-01 16:58:55.716: E/AndroidRuntime(483): android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197) 06-01 16:58:55.716: E/AndroidRuntime(483): android.os.Handler.dispatchMessage(Handler.java:99) 06- 01 16:58:55.716: E/AndroidRuntime(483): android.os.Looper で。ループ (Looper.java:137) 06-01 16:58:55.716: E/AndroidRuntime(483): android.app.ActivityThread.main(ActivityThread.java:4424) 06-01 16:58:55.716: E/ AndroidRuntime(483): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) 06-01 16:58:55.716: E/AndroidRuntime(483): java.lang.reflect.Method.invoke(Method.java: 511) 06-01 16:58:55.716: E/AndroidRuntime(483): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 06-01 16:58:55.716: E/ AndroidRuntime(483): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-01 16:58:55.716: E/AndroidRuntime(483): dalvik.system.NativeStart.main(ネイティブ)メソッド) 06-01 16:58:55.716: E/AndroidRuntime(483): 原因: java.lang.ClassNotFoundException: it.bloomp.activity.receiver.StartEventsNotificationService 06-01 16:58:55.716:E/AndroidRuntime(483): dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 06-01 16:58:55.716: E/AndroidRuntime(483): java.lang.ClassLoader.loadClass(ClassLoader.java) :501) 06-01 16:58:55.716: E/AndroidRuntime(483): java.lang.ClassLoader.loadClass(ClassLoader.java:461) 06-01 16:58:55.716: E/AndroidRuntime(483): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2095) 06-01 16:58:55.716: E/AndroidRuntime(483): ... 10 詳細ActivityThread.handleReceiver(ActivityThread.java:2095) 06-01 16:58:55.716: E/AndroidRuntime(483): ... 10 もっと見るActivityThread.handleReceiver(ActivityThread.java:2095) 06-01 16:58:55.716: E/AndroidRuntime(483): ... 10 もっと見る
EDIT2:
私のマニフェスト:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.bloomp.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
<activity
android:name=".EventsListActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EventActivity"/>
<service android:name=".EventsNotificationService" android:label="EventsNotificationService">
<intent-filter>
<action android:name="it.bloomp.service.EventsNotificationService" />
</intent-filter>
</service>
<receiver
android:name=".StartEventsNotificationService"
android:enabled="true"
android:exported="true"
android:label="StartEventsNotificationService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
私のサービス:
package it.bloomp.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class EventsNotificationService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("Service created.");
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("Service destroyed.");
}
@Override
public void onStart(Intent intent, int startId) {
super.onCreate();
System.out.println("Service started.");
}
}
私のサービススターター:
package it.bloomp.service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartEventsNotificationService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceIntent = new Intent("it.bloomp.service.EventsNotificationService");
context.startService(serviceIntent);
}
}
}
私の例外:
06-03 15:37:36.142: E/AndroidRuntime(479): 致命的な例外: メイン 06-03 15:37:36.142: E/AndroidRuntime(479): java.lang.RuntimeException: レシーバー it.bloomp をインスタンス化できません。 activity.StartEventsNotificationService: java.lang.ClassNotFoundException: it.bloomp.activity.StartEventsNotificationService 06-03 15:37:36.142: E/AndroidRuntime(479): android.app.ActivityThread.handleReceiver(ActivityThread.java:2100) で 06- 03 15:37:36.142: E/AndroidRuntime(479): android.app.ActivityThread.access$1500(ActivityThread.java:123) で 06-03 15:37:36.142: E/AndroidRuntime(479): android.app で.ActivityThread$H.handleMessage(ActivityThread.java:1197) 06-03 15:37:36.142: E/AndroidRuntime(479): android.os.Handler.dispatchMessage(Handler.java:99) 06-03 15:37 :36.142: E/AndroidRuntime(479): android.os.Looper.loop(Looper.java:137) 06-03 15:37:36.142: E/AndroidRuntime(479): android.app.ActivityThread.main(ActivityThread.java:4424) 06-03 15:37:36.142: E/AndroidRuntime(479) : java.lang.reflect.Method.invokeNative(Native Method) 06-03 15:37:36.142: E/AndroidRuntime(479): java.lang.reflect.Method.invoke(Method.java:511) 06- 03 15:37:36.142: E/AndroidRuntime(479): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 06-03 15:37:36.142: E/AndroidRuntime(479) : com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-03 15:37:36.142: E/AndroidRuntime(479): dalvik.system.NativeStart.main(ネイティブ メソッド) 06- 03 15:37:36.142: E/AndroidRuntime(479): 原因: java.lang.ClassNotFoundException: it.bloomp.activity.StartEventsNotificationService 06-03 15:37:36.142: E/AndroidRuntime(479):dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 06-03 15:37:36.142: E/AndroidRuntime(479): java.lang.ClassLoader.loadClass(ClassLoader.java:501) 06-03 15 :37:36.142: E/AndroidRuntime(479): java.lang.ClassLoader.loadClass(ClassLoader.java:461) 06-03 15:37:36.142: E/AndroidRuntime(479): android.app.ActivityThread で。 handleReceiver(ActivityThread.java:2095) 06-03 15:37:36.142: E/AndroidRuntime(479): ... 10 もっと見るhandleReceiver(ActivityThread.java:2095) 06-03 15:37:36.142: E/AndroidRuntime(479): ... 10 もっと見るhandleReceiver(ActivityThread.java:2095) 06-03 15:37:36.142: E/AndroidRuntime(479): ... 10 もっと見る