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.!!