0

I am trying to start service on Boot completion, After doing lot digging I have found nothing.

Very first approach that I have tried was this, I found this approach in most answers, but it doesn't work.

Then I have found that this approach is not working on 3.1+ from here

I have also seen this, https://developer.android.com/about/versions/android-3.1.html#launchcontrols

and used FLAG_INCLUDE_STOPPED_PACKAGES also in my code.I have referred this approach from here.

AndroidMenifest

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REAL_GET_TASKS"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/security"
    android:label="@string/app_name"
    android:supportsRtl="true">

    <receiver android:name="com.reversebits.projects.app.easyerase.receiver.BootReceiver"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
        android:enabled="true"
        android:exported="true">

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

        <intent-filter>
            <action android:name="myReceiver" />
        </intent-filter>

    </receiver>

    <activity
        android:name=".Home"
        android:theme="@style/AppTheme">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

    <service
        android:name=".services.BootService"
        android:enabled="true">

        <intent-filter>
            <action android:name="com.reversebits.projects.app.easyerase.services.BootService"/>
        </intent-filter>

    </service>

</application>

BootReciever

public class BootReceiver extends BroadcastReceiver {

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

        if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
        {
            context.startService(new Intent(context, BootService.class));
        }
    }
}

BootService

public class BootService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //TODO do something useful Recommended by google instead of onStart()
        return Service.START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        loopThread();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    void loopThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                switch (am.getRingerMode()) {
                    case AudioManager.RINGER_MODE_SILENT:
                        Log.e("MyApp", "Silent mode");
                        break;
                    case AudioManager.RINGER_MODE_VIBRATE:
                        Log.e("MyApp", "Vibrate mode");
                        break;
                    case AudioManager.RINGER_MODE_NORMAL:
                        Log.e("MyApp", "Normal mode");
                        break;

                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                loopThread();
            }
        }).start();
    }
}

Code works perfect and i got all logs when service is starting from activity, but on Boot complete, it is not working

Note: I have read almost all related answers and on some answers,I have also read comments and I know almost all related things, so please do not provide me other links, as have seen most of them already, I need the perfect solution. Any help is appreciated.

Community
  • 1
  • 1
TapanHP
  • 5,969
  • 6
  • 37
  • 66
  • Can you try adding another action – Nayan Srivastava Nov 04 '16 at 10:46
  • okay @NayanSrivastava – TapanHP Nov 04 '16 at 10:51
  • still not working @NayanSrivastava – TapanHP Nov 04 '16 at 10:54
  • did you check permissions? – Rushi Ayyappa Nov 04 '16 at 11:01
  • yes of cource !! You can see menifest file code @RushiAyyappa – TapanHP Nov 04 '16 at 11:03
  • What device are you testing this on? Have you tried other devices? – David Wasser Nov 04 '16 at 11:04
  • It's Xolo Q510s , With Kitkat 4.4.2 flavour @DavidWasser – TapanHP Nov 04 '16 at 11:05
  • First, remove the `permission` attribute from the `` element. Then, make sure that you're running your `Activity` at least once after installation, before rebooting. And, if you're testing on a device, be aware that some OEMs restrict apps from running at startup by default. You'll need to look in your settings - or possibly in any security-related apps it might have - to see if that's the case. Also note that it may take up to several minutes for your Receiver to actually get the boot broadcast. – Mike M. Nov 04 '16 at 11:06
  • 4
    Just a suggestion. In the future, if you don't want to get downvotes, do not tell people to "read it all before downvoting". Do not write "all my code works perfectly except...". Do not write "I have read all the answers and comments so please don't post links to them". This makes you come across as an arrogant know-it-all, but since you have a problem you want us to help you solve, you obviously don't know it all. So in the future, when asking a question, try to be humble. It will save you some downvotes. – David Wasser Nov 04 '16 at 11:07
  • okay @MikeM. I have removed , and after waiting for some minutes, it's still not receiving broadcast, but how can i check it in my settings? Should i add Admin authority in my app ? – TapanHP Nov 04 '16 at 11:09
  • On some devices there is a separate security settings screen where you need to explicitly enable apps to be started at boot time. It also seems that there are some devices that just refuse to start 3rd party applications at boot, for whatever reason. I strongly suggest that you find another device to test on to be sure. – David Wasser Nov 04 '16 at 11:10
  • Just remember, you are asking us to help you. For free (without having to pay for it). Think about that for a minute. – David Wasser Nov 04 '16 at 11:12
  • @DavidWasser not working on Samsung device also, It has jelly bin flovour, but not working at all – TapanHP Nov 04 '16 at 11:19

1 Answers1

2
  <receiver android:name=".BootCompletedIntentReceiver"
        android:enabled="true"
        android:exported="true"
        >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service android:name=".Service"
        android:enabled="true"
        android:exported="true">

    </service>

yo

          public class BootCompletedIntentReceiver extends BroadcastReceiver {
   public void onReceive(Context context, Intent intent) {

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent pushIntent = new Intent(context, Service.class);

        context.startService(pushIntent);}

and dont forget

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

service

public class BootService extends Service {
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStart(Intent intent, int startId) {
    //TODO do something useful Recommended by google instead of onStart()
     new Thread(new Runnable() {
        @Override
        public void run() {
            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            switch (am.getRingerMode()) {
                case AudioManager.RINGER_MODE_SILENT:
                    Log.e("MyApp", "Silent mode");
                    break;
                case AudioManager.RINGER_MODE_VIBRATE:
                    Log.e("MyApp", "Vibrate mode");
                    break;
                case AudioManager.RINGER_MODE_NORMAL:
                    Log.e("MyApp", "Normal mode");
                    break;

            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }).start();
}




@Override
public void onDestroy() {
    super.onDestroy();
}
  }