0

So, this is a Lollipop app (5.0) which has a boot receiver. The boot receiver starts an activity (SplashActivity.class). The purpose of SplashActivity is to get all necessary permissions prior to launching the MainActivity.class.

public class SplashActivity extends Activity {

private static boolean bPermission = false;
private static final int MY_PERMISSIONS_REQUEST = 100;
private static final String[] allRequestedPermissions = new String[] {
        Manifest.permission.READ_CONTACTS,
        Manifest.permission.SEND_SMS,
        Manifest.permission.CALL_PHONE
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(getIntent().hasExtra("PERMISSION_FOR_SERVICE")) {
        bPermission = true;
    }
    if (!checkAllRequestedPermissions()) {
        ActivityCompat.requestPermissions(this, allRequestedPermissions, MY_PERMISSIONS_REQUEST);
    }
    else {
        startMainActivity();
    }
}

private boolean checkAllRequestedPermissions() {
    for (String permission : allRequestedPermissions) {
        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
    }
    return true;
}

private boolean isServiceRunning(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
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST: {
            if (checkAllRequestedPermissions()) {
                    startMainActivity();
            }
            else {
                finish();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

private void startMainActivity() {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
    if(bPermission) {
        intent.putExtra("PERMISSION_FOR_SERVICE","_");
    }
    startActivity(intent);
}

}

What ends up happening after all the permission checking and requesting (if you scroll to the bottom) is that it starts MainActivity.

The issue is I would rather for it not to start MainActivity in the case where bPermission == true. If bPermission == true, would prefer the following behavior:

private void startMainActivity() {
    if(bPermission) {
        startService(new Intent(this, SchedulerService.class));
        finish();
    }
    else {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);

        intent.putExtra("PERMISSION_FOR_SERVICE", "_");
        startActivity(intent);
    }

}

But, this crashes the application. Any ideas what might be wrong? Many thanks in advance

RESOLVED: My service had a dependency was not aware of. Initializing dependency prior to starting service fixed issue! Thanks @mike m.!!

mike morris
  • 174
  • 7
  • Look at [the stack trace](https://stackoverflow.com/questions/23353173) to determine the cause of the crash. – Mike M. Dec 17 '18 at 02:40
  • Checking now if the debugger will catch on startup.. – mike morris Dec 17 '18 at 02:43
  • Didn't get that far: it says null pointer on the SchedulerService – mike morris Dec 17 '18 at 02:45
  • Where is the Exception happening? In the `Service`? – Mike M. Dec 17 '18 at 02:47
  • Process: com.better_computer.habitaid, PID: 6810 java.lang.RuntimeException: Unable to instantiate service com.better_computer.habitaid.scheduler.SchedulerService: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference at android.app.ActivityThread.handleCreateService(ActivityThread.java:3088) – mike morris Dec 17 '18 at 02:51
  • Hmm, doesn't look familiar, at first glance. Please [edit] your question to provide the complete stack trace. – Mike M. Dec 17 '18 at 02:55
  • 1
    It looks like you're instantiating that `DatabaseHelper` in a field initializer in your `Service`. That is, you've got something like `private DatabaseHelper helper = new DatabaseHelper(this);` outside of any class method in `SchedulerService`. You can't use the `Service` as a `Context` there, as the `Context` will not yet have been set on the `Service` instance. Move that instantiation into a method; e.g., `onCreate()`. – Mike M. Dec 17 '18 at 03:08
  • 1
    That was it, just had to initialize DatabaseHelper prior to starting service!! :-D :) THANK YOU SOOOO MUCH! – mike morris Dec 17 '18 at 03:11

1 Answers1

0

RESOLVED: My service had a dependency was not aware of. Initializing dependency prior to starting service fixed issue! Thanks @Mike M.!!

mike morris
  • 174
  • 7