I`m having problems when trying to start a service on android start up, as soon as the phone starts my app gives an error and has to stop it. Here it is:
part of my manifest:
<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>
my service:
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();
}
}
my service starter:
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);
}
}
}
Also, this make the service start on start up, but how can I make it run all time?
EDIT:
The exceptions:
06-01 16:58:55.716: E/AndroidRuntime(483): FATAL EXCEPTION: main 06-01
16:58:55.716: E/AndroidRuntime(483): java.lang.RuntimeException:
Unable to instantiate receiver
it.bloomp.activity.receiver.StartEventsNotificationService:
java.lang.ClassNotFoundException:
it.bloomp.activity.receiver.StartEventsNotificationService 06-01
16:58:55.716: E/AndroidRuntime(483): at
android.app.ActivityThread.handleReceiver(ActivityThread.java:2100)
06-01 16:58:55.716: E/AndroidRuntime(483): at
android.app.ActivityThread.access$1500(ActivityThread.java:123) 06-01
16:58:55.716: E/AndroidRuntime(483): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
06-01 16:58:55.716: E/AndroidRuntime(483): at
android.os.Handler.dispatchMessage(Handler.java:99) 06-01
16:58:55.716: E/AndroidRuntime(483): at
android.os.Looper.loop(Looper.java:137) 06-01 16:58:55.716:
E/AndroidRuntime(483): at
android.app.ActivityThread.main(ActivityThread.java:4424) 06-01
16:58:55.716: E/AndroidRuntime(483): at
java.lang.reflect.Method.invokeNative(Native Method) 06-01
16:58:55.716: E/AndroidRuntime(483): at
java.lang.reflect.Method.invoke(Method.java:511) 06-01 16:58:55.716:
E/AndroidRuntime(483): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-01 16:58:55.716: E/AndroidRuntime(483): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-01
16:58:55.716: E/AndroidRuntime(483): at
dalvik.system.NativeStart.main(Native Method) 06-01 16:58:55.716:
E/AndroidRuntime(483): Caused by: java.lang.ClassNotFoundException:
it.bloomp.activity.receiver.StartEventsNotificationService 06-01
16:58:55.716: E/AndroidRuntime(483): at
dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-01 16:58:55.716: E/AndroidRuntime(483): at
java.lang.ClassLoader.loadClass(ClassLoader.java:501) 06-01
16:58:55.716: E/AndroidRuntime(483): at
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 more
EDIT2:
My manifest:
<?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>
My service:
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.");
}
}
My service starter:
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);
}
}
}
My exceptions:
06-03 15:37:36.142: E/AndroidRuntime(479): FATAL EXCEPTION: main 06-03
15:37:36.142: E/AndroidRuntime(479): java.lang.RuntimeException:
Unable to instantiate receiver
it.bloomp.activity.StartEventsNotificationService:
java.lang.ClassNotFoundException:
it.bloomp.activity.StartEventsNotificationService 06-03 15:37:36.142:
E/AndroidRuntime(479): at
android.app.ActivityThread.handleReceiver(ActivityThread.java:2100)
06-03 15:37:36.142: E/AndroidRuntime(479): at
android.app.ActivityThread.access$1500(ActivityThread.java:123) 06-03
15:37:36.142: E/AndroidRuntime(479): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
06-03 15:37:36.142: E/AndroidRuntime(479): at
android.os.Handler.dispatchMessage(Handler.java:99) 06-03
15:37:36.142: E/AndroidRuntime(479): at
android.os.Looper.loop(Looper.java:137) 06-03 15:37:36.142:
E/AndroidRuntime(479): at
android.app.ActivityThread.main(ActivityThread.java:4424) 06-03
15:37:36.142: E/AndroidRuntime(479): at
java.lang.reflect.Method.invokeNative(Native Method) 06-03
15:37:36.142: E/AndroidRuntime(479): at
java.lang.reflect.Method.invoke(Method.java:511) 06-03 15:37:36.142:
E/AndroidRuntime(479): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-03 15:37:36.142: E/AndroidRuntime(479): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-03
15:37:36.142: E/AndroidRuntime(479): at
dalvik.system.NativeStart.main(Native Method) 06-03 15:37:36.142:
E/AndroidRuntime(479): Caused by: java.lang.ClassNotFoundException:
it.bloomp.activity.StartEventsNotificationService 06-03 15:37:36.142:
E/AndroidRuntime(479): at
dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-03 15:37:36.142: E/AndroidRuntime(479): at
java.lang.ClassLoader.loadClass(ClassLoader.java:501) 06-03
15:37:36.142: E/AndroidRuntime(479): at
java.lang.ClassLoader.loadClass(ClassLoader.java:461) 06-03
15:37:36.142: E/AndroidRuntime(479): at
android.app.ActivityThread.handleReceiver(ActivityThread.java:2095)
06-03 15:37:36.142: E/AndroidRuntime(479): … 10 more
android:name=”.receiver.StartEventsNotificationService”
probably should be
android:name=”.StartEventsNotificationService”
EDIT
try this as your intent:
Intent i = new Intent(context, EventNotificationService.class);
also, you should update the error you received. It can’t be the same as what you have, given what you’ve shown from your manifest and service starter code. Try to read the errors yourself and follow where they point to, learning how to do that efficiently will save you tons of time on StackOverflow and will teach you a lot better than just asking others.