1

If my apps already running in background service and after that close this apps in recent apps and the problem is my apps can't start again in background service. this problem only on other device (e.g xiaomi mi4i), another device can running without problem.

-Service

public class MyService extends Service {

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started...", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service Destroyed...", Toast.LENGTH_LONG).show();
        onCreate();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

-MainActivity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void startService(View view) {
    Intent intent = new Intent(this, MyService.class);
    startService(intent);
}

public void stopService(View view) {
    Intent intent = new Intent(this, MyService.class);
    stopService(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
  • Have you checked what permission the Xaiomi device gives to your app? Settings-Apps-Your App? Xaiomi sucks in various profound ways, in that its more of an iOS dupe than an Android phone. – Skynet Dec 17 '15 at 03:49

3 Answers3

1

Xiaomi provide an 'Auto-Start' option under its security app. You have to manually toggle this option to ON. Unless this is done, your app will not be able to perform any background tasks if it is killed and you will not be able to perform simple background operations like getting push notification, updating location etc. This option enables the Services to be restarted in case they are killed in the background. Any background services such as GCM Periodic task, AlarmManager will be useless until auto start option is on.

Aman Kaushik
  • 360
  • 2
  • 10
0

I am also facing same issue for Mi 4i, scheduled a Alarm Manager using following way but not starting.

AlarmManager am = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
Intent intent = new Intent(this, CheckReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
am.setExact(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + 5000), pendingIntent);
-1

you can use Alarm Manager to start service if it not start propely as automaticly.

Old this post help you do that How to start Service using Alarm Manager in Android?

Community
  • 1
  • 1
GiapLee
  • 436
  • 2
  • 8
  • Hi @GiapLee : I have try with alarm manager but same issue found , any suggestion for run successful background service after an app kill in (e.g xiaomi mi4i) devices. – Lokesh Patel Jan 11 '16 at 07:29