0

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.

Logan Guo
  • 865
  • 4
  • 17
  • 35

3 Answers3

1

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>
M D
  • 47,665
  • 9
  • 93
  • 114
  • I changed the code according to your description, turned out still failed to print logs. – Logan Guo Sep 16 '14 at 10:24
  • @LoganGuo No No it's not possible. you still doing some mistake...post your all manifest and some more code... – M D Sep 16 '14 at 10:25
  • @LoganGuo or refer this [stackoverflow.com/questions/7690350/android-start-service-on-boot?rq=1](http://stackoverflow.com/questions/7690350/android-start-service-on-boot?rq=1) – M D Sep 16 '14 at 10:27
  • @M D I already posted more code, if you refresh you can view it. After comparing the reference you supplied, I found the order of Service and Receiver Tag is not the same, should I move the Receiver tag to the bottom of Service tag? Thanks by the way – Logan Guo Sep 16 '14 at 10:37
  • my testing phone is Samsung p3110 – Logan Guo Sep 16 '14 at 10:40
  • @LoganGuo Move your root receiver after activity and services and try.... i am not sure about this..... – M D Sep 16 '14 at 10:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61333/discussion-between-m-d-and-logan-guo). – M D Sep 16 '14 at 10:43
  • @M D thanks for your advice, I found that I didn`t register the receiver yet. now it works. – Logan Guo Sep 16 '14 at 11:01
  • @M D by the way, I didn`t figure out how to register the receiver here yet, do I need to register the receiver in launcher activity. Also I think I already registered it in Manifest.xml, so could you please show more info about your meaning of registration we talked about yesterday. I will really appreciate that. – Logan Guo Sep 17 '14 at 03:07
0

Correct receiver entry in your Manifest should be just

<receiver android:name=".OSBootReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0
What is package name used in file OSBootReceiver.java

If package name is  com.seafile.seadroid2,


**change Receiver in manifest as below:**

<receiver android:name="com.seafile.seadroid2.OSBootReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

I added like above, and i am getting Boot completed broadcast intent.

For testing purpose, 
add Toast message in onRecieve method:


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

    @Override
    public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "boot completed", Toast.LENGTH_SHORT).show();
    Log.e(DEBUG_TAG,"boot completed intent received");
}
}
Satya Ch
  • 29
  • 4