Does anyone know why my service cannot be started when i boot up my Android phone? I am trying to link my service to my activity class, and when the activity crashes, the service will relaunch the activity again. My service class is as follows
public class Restarter extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.v("Restarter", "Restarter Created");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Stamp_Client sc = new Stamp_Client();
System.out.println("Restarter Service Started");
while(true){
System.out.println("Checking for crash...");
int instance;
instance = sc.nbInstances();
if(instance == 0){
Intent myIntent =
new Intent(Restarter.this,Stamp_Client.class);
Restarter.this.startActivity(myIntent);
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
} }
My manifest is as follows
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.i2r.cas.mofa.main"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS">
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
</uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Stamp_Client"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize"
android:configChanges="orientation|keyboardHidden">
<service android:enabled="true" android:name=".Restarter" >
<intent-filter>
<action android:name="com.i2r.cas.mofa.main.Restarter">
</action>
</intent-filter>
</service>
<receiver android:name=".StartAtBootServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.开发者_开发百科intent.category.HOME">
</category>
</intent-filter>
</receiver>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Does anyone know why this doesn't get called during bootup? Thanks!
In your manifest file you declared the element in the wrong manner.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.i2r.cas.mofa.main"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS">
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
</uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Stamp_Client"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".Restarter" >
<intent-filter>
<action android:name="com.i2r.cas.mofa.main.Restarter">
</action>
</intent-filter>
</service>
<receiver android:name=".StartAtBootServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
</manifest>
In your StartBootServiceReceiver
call the startService(intent of the service class) method
精彩评论