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);
}
}