0

I have been trying to workout this problem for the past couple of hours looking at various code samples here but still haven't found a solution.

I want a service to start when app starts and when the phone is reboot the service should start again automatically without starting the app. (Neither the toast or log appears when i restart my phone). Please see my code below:

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(this, MyService.class);
    startService(intent);
}

MyService.java

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

 @Override
 public int onStartCommand(Intent intent, int flags, int startid){
    Toast.makeText(this, "Service Running ...", Toast.LENGTH_LONG).show();
    Log.v("Service","Service Running ...");

    return super.onStartCommand(intent, flags, startid);
 }
}

Autostart.java

public class Autostart extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
    Intent myIntent = new Intent(context, MyService.class);
    context.startService(myIntent);
 }
}

AndroidManifest.xml

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

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

   <service android:name=".MyService" />

</application>
</manifest>
stud91
  • 1,854
  • 6
  • 31
  • 56
  • Possible duplicate of http://stackoverflow.com/questions/6391902/how-to-start-an-application-on-startup. See there for more hints, in any case. – dhke Mar 11 '15 at 08:33
  • @dhke ive already seen that before – stud91 Mar 11 '15 at 08:35
  • What I see there is `android:permission="android.permission.RECEIVE_BOOT_COMPLETED`. Does that change anything for you? – dhke Mar 11 '15 at 08:36
  • Possibly duplicate of http://stackoverflow.com/questions/4562734/android-starting-service-at-boot-time – VVB Mar 11 '15 at 08:40
  • Are you testing it on HTC device? If yes, then you need to add BOOT_CATCH_COMPLETED permission too. – VVB Mar 11 '15 at 08:43
  • @VVB I am testing on Xiaomi..and which permission is this.. i cannot figure out – stud91 Mar 11 '15 at 08:50
  • http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android – VVB Mar 11 '15 at 08:54
  • @VVB Comparing that answer i don't see what is wrong with my code – stud91 Mar 11 '15 at 09:01
  • your code is working fine. i just change android:name="android.intent.action.BOOT_COMPLETED" to android.intent.action.ACTION_POWER_CONNECTED – Null Pointer Exception Jun 22 '18 at 06:05

1 Answers1

0

Seems like this is a bug on Xiaomi. I haven't encountered this problem with any other device

stud91
  • 1,854
  • 6
  • 31
  • 56