I have created an app where it increments a number stored in the database and updates the number in the database. And when the app is killed a broadcast is sent and the receiver broadcast does the same above operation. The problem that I am facing is that when the app is opened it looks whether a background service is running or not. But when an background service is actually running it is not able to detect even though I wrote a method for it. I get the following error
2019-12-11 00:40:24.061 21588-21588/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 21588
java.lang.RuntimeException: Unable to start receiver com.example.myapplication.SensorRestarterBroadcastReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.example.myapplication/.MyService }: app is in background uid UidRecord{42ec1a7 u0a240 RCVR idle procs:1 seq(0,0,0)}
Here are my main activity and Service activity.
Main Activity.java
public class MainActivity extends AppCompatActivity {
TextView t1;
DatabaseAccess databaseAccess;
Intent mServiceIntent;
private MyService mSensorService;
Context ctx;
public Context getCtx() {
return ctx;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = getApplicationContext();
setContentView(R.layout.activity_main);
int value = 0;
databaseAccess=DatabaseAccess.getInstance(getApplicationContext());
databaseAccess.open();
value=databaseAccess.readValue();
databaseAccess.close();
t1=findViewById(R.id.text);
t1.setText(""+value);
mSensorService = new MyService(getCtx());
mServiceIntent = new Intent(getCtx(), MyService.class);
boolean stats=isMyServiceRunning(MyService.class);
if (!stats) {
startService(mServiceIntent);
}
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
@Override
protected void onDestroy() {
super.onDestroy();
stopService(mServiceIntent);
}
}
MyService.java
public class MyService extends Service {
public int inValue;
DatabaseAccess databaseAccess;
public MyService(Context applicationContext) {
super();
}
public MyService() {
}
@Override
@Nullable
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
//Read the database and increment the value in the counter which is available
databaseAccess = DatabaseAccess.getInstance(getApplicationContext());
databaseAccess.open();
inValue = databaseAccess.readValue();
databaseAccess.close();
incremental(inValue);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Intent broadcastIntent = new Intent(this,SensorRestarterBroadcastReceiver.class);
sendBroadcast(broadcastIntent);
}
public void incremental(int i) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
i=i+1;
databaseAccess=DatabaseAccess.getInstance(getApplicationContext());
databaseAccess.open();
databaseAccess.updateValue(i);
databaseAccess.close();
}
}