Search code examples
javaandroidboo

failed to start service on boot


my App has a camera photo backup service. When user turned on the service, it will automatically backup sd-card photos to remote server. But when user shut down android OS and boot again, the backup service failed to start again.

I read many articles online, I did exactly the same as they described, but turned out to be wrong. Maybe some experts here can help me. see the code below.

In android manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.seafile.seadroid2"
      android:versionCode="20"
      android:versionName="1.0.1"
      android:installLocation="internalOnly">              
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />    
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />        
    <application
        android:allowBackup="true"
        android:name="com.seafile.seadroid2.SeadroidApplication"
        android:label="@string/app_name"
        android:icon="@drawable/ic_launcher">            
        <receiver android:name=".OSBootReceiver" >
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />  
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <activity android:name="com.seafile.seadroid2.BrowserActivity"
                  android:label="@string/app_name"
                  android:theme="@style/Theme.SeafileTheme"
                  android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.seafile.seadroid2.transfer.TransferService" > </service>
        <service android:name="com.seafile.seadroid2.monitor.FileMonitorService" > </service>
        <service android:name="com.seafile.seadroid2.sync.CameraUploadService" > </service>            
    </application>
</manifest>

OSBootReceiver class

 public class OSBootReceiver extends BroadcastReceiver {
    private static final String DEBUG_TAG = "OSBootReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(DEBUG_TAG, "boot to notic receiver");

        Intent cameraUploadIntent = new Intent(context,
                CameraUploadService.class);
        SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        boolean isUploadStart = settings.getBoolean(
                BrowserActivity.CAMERA_UPLOAD_SWITCH_KEY, false);
        if (!isUploadStart) {
            return;
        }
        Log.d(DEBUG_TAG, "boot to start service");
        context.startService(cameraUploadIntent);
    }

}

this it the complete project with start service error on github

I tried to reboot os many times, but didnt fond any log printed! any advice will be appreciated.


Solution

  • Try to check Boot Completed action in onReceive(.....) like

    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
    
       Logs("ON Boot completed with System BroadcastReceiver");
    
      //do your job
    
     }
    

    and also set <intent-filter> like to your OSBootReceiver in manifest.xml

           <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>