1

I have read several answers about this question, but the posted solution doesn't work for me. Probably there is something wrong or missed in my code. I need that my app, with no activity, starts automatically after the boot completed. If I include an activty, just to start for the first time the app (exiting the stopped state), everything works. Thank you in advance for your help.

Here is my code.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="zag.salva" >

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver
        android:name=".Salva_autostart"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service
        android:name=".Salva_servizio"
        android:enabled="true" >
        <intent-filter>
            <action android:name=".Salva_servizio" />
        </intent-filter>
    </service>

</application>

Salva_autostart.java

public class Salva_autostart extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent intento = new Intent(context, Salva_servizio.class);
        intento.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        context.startService(intento);
    }


}

Salva_servizio.java

public class Salva_servizio extends Service
{
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        // Task execution
        Salva_invio2 invio = new Salva_invio2();
        invio.esegui(this);
        return Service.START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent)
    {
       return null;
    }
}
MarioZ
  • 11
  • 1
  • 2

2 Answers2

4

You shouldn’t add FLAG_INCLUDE_STOPPED_PACKAGES to your receiver's intent that start your service. You have to add it to the intent that you use for sendBroadcast. Meaning, you need to add it to the intent in the application that invokes the Broadcast. That is why this flag is irrelevant in your code.

If you will sendBroadcast to this receiver ("Salva_autostart") just once, from outside your application - then your application will not be in "force stop" state any more and on next boot your receiver will be triggered.

Also you should add (addFlags) and not set (setFlags).

 intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);

This is how you should trigger your receiver from another application:

   Intent intent = new Intent("com.xxx.my_filter_intent");
   intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
   this.sendBroadcast(intent);

On your manifest add the above filter intent to your receiver (you can add it in new <intent-filter> or in the same that you already have for the BOOT_COMPLETED action.

<receiver
    android:name=".Salva_autostart"
    android:enabled="true"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >

    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
    <intent-filter>
        <action android:name="com.xxx.my_filter_intent" />
    </intent-filter>
</receiver>

Read more here: http://developer.android.com/about/versions/android-3.1.html#launchcontrols

Eric Platon
  • 9,819
  • 6
  • 41
  • 48
Udi Reshef
  • 1,083
  • 1
  • 11
  • 14
  • Is it keep my service running in background even after force close app from device settings ? – Himanshu Jan 20 '17 at 15:51
  • Hi HimCream, It will not keep the service running after force stop, having this flag in the broadcast will just trigger your receiver when the broadcast will be sent again (even if youer app is in force stop atate). Then, if your receiver start the service – your service will start again. – Udi Reshef Jan 23 '17 at 09:35
0

Note that as of Android 3.0 the user needs to have started the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.

Mimmo Grottoli
  • 5,758
  • 2
  • 17
  • 27
  • But this would be the default behavior; that's why I introduce the setFlags() statement; It isn't like this ? Thanks – MarioZ Jul 20 '15 at 11:23
  • 1
    Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications). The system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents by default, and your BroadcastReceiver is listening for a system broadcast. So basically your Salva_autostart will not get any Intent if the user does not launch your app. If it does not get any Intent, it cannot start the Service as well. – Mimmo Grottoli Jul 20 '15 at 11:34
  • Are you still thinking? My answer is right? If so, rate or vote please. – Mimmo Grottoli Jul 21 '15 at 20:22
  • Apparently it's starting **Android 3.1** http://stackoverflow.com/a/19856267/1630447 – Snaker Apr 17 '16 at 19:49