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.