0

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();
    }
}
Gautam
  • 1
  • 1
  • Does this answer your question? [Not allowed to start service Intent - Android Oreo](https://stackoverflow.com/questions/51353493/not-allowed-to-start-service-intent-android-oreo) – greeble31 Dec 10 '19 at 19:55
  • @greeble31 But can I use JobIntent to run a service when my app is killed by user? – Gautam Dec 23 '19 at 17:45
  • You may want to ask a separate question; I can't see what that has to do with this one. – greeble31 Dec 23 '19 at 18:04
  • @greeble31 I want to the same thing that MyService class is doing but in JobIntent since API levels after 26 you have to use the JobScheduler to run your app in background. I couldn't find anything that will help me to run my service or app in background even if it is killed by the user through JobIntent. – Gautam Dec 23 '19 at 18:11
  • then just ask a separate question. You can ask multiple questions on this site. – greeble31 Dec 23 '19 at 18:15

0 Answers0