0

thanks to everyone for reading. here's an application that has a boot-receiver so it launches upon starting android device:

public class BootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Intent i1 = new Intent(context, SplashActivity.class);
    i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i1.putExtra("permission_for_service","");
    context.startActivity(i1);

}

SplashActivity.class is implemented to obtain permissions before launching MainActivity.class.

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

    if (!checkAllRequestedPermissions()) {
        ActivityCompat.requestPermissions(this, allRequestedPermissions, MY_PERMISSIONS_REQUEST);
    }
    else {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }
}    

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

What I'm trying to do is to prevent launching a user interface on boot-up by using a service instead:

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

    if (!checkAllRequestedPermissions()) {
        ActivityCompat.requestPermissions(this, allRequestedPermissions, MY_PERMISSIONS_REQUEST);
    } else {
        if (getIntent().hasExtra("permission_for_service")) {
            //service init
            if (!isServiceRunning(SchedulerService.class)) {
                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);
            startActivity(intent);
        }
    }
}

Unfortunately, this crashes on BOOT_COMPLETE. Any ideas on how to make it work? Thanks a million

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
mike morris
  • 174
  • 7
  • Use Logcat to examine the Java stack trace associated with the crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Dec 16 '18 at 20:53
  • Would do but this happens when device boots, so its not yet linked with a debugger / emulator – mike morris Dec 16 '18 at 20:55
  • 1
    Logcat is still collected even during a boot process, for you to examine once the device is connected. Or, launch the broadcast receiver manually using `adb` rather than rebooting. – CommonsWare Dec 16 '18 at 20:56
  • if i launch it manually, it would be from within the app, so it would have already obtained permissions – mike morris Dec 16 '18 at 20:57
  • "if i launch it manually, it would be from within the app" -- no, it would be from the command line of your development machine. "so it would have already obtained permissions" -- revoke them from the Settings app. – CommonsWare Dec 16 '18 at 20:58
  • fair, but a bit green for this. how to trigger the broadcast in adb? how to use logcat to obtain meaningful information in this case about why its failing? – mike morris Dec 16 '18 at 20:59
  • and what is `allRequestedPermissions`? – Martin Zeitler Dec 16 '18 at 21:02
  • private static final String[] allRequestedPermissions = new String[] { Manifest.permission.READ_CONTACTS, Manifest.permission.SEND_SMS, Manifest.permission.CALL_PHONE }; – mike morris Dec 16 '18 at 21:03
  • "how to trigger the broadcast in adb?" -- See https://developer.android.com/studio/command-line/adb#am. Use `adb shell am broadcast -n com.example.app/.YourReceiver`, customizing that package name and class accordingly. "how to use logcat to obtain meaningful information in this case about why its failing?" -- if you are crashing, there will be a Java stack trace with details of the exception. "but a bit green for this" -- you might want to consider starting with a simpler project. – CommonsWare Dec 16 '18 at 21:03
  • 1
    @mikemorris just don't start the service, while permissions had not yet been granted. and maybe check, what the `context` is in that scope... because that it crashes hints for, that it might be `null`... or not a context, which could be used. the `Exception` from the log-cat would tell for certain. – Martin Zeitler Dec 16 '18 at 21:04
  • @MartinZeitler, i think you're on the right track. I tried moving the code into the callback function onRequestPermissionsResult(, but finish() is still not executing from within that function. Probably it's the context, will continue to investigate.. – mike morris Dec 16 '18 at 21:18
  • @mikemorris https://stackoverflow.com/a/42189308/549372 – Martin Zeitler Dec 16 '18 at 21:54
  • @MartinZeitler, you were right it was crashing b/c service was launching before it got permissions. Got it to work by launching activity briefly, checking for bool in onCreate and running finish() but now have an ugly flash of white screen (will do for my purposes for now). Thank you so much! Please let me know how to convey points. Cheers – mike morris Dec 16 '18 at 21:57

0 Answers0