Lê Khánh Vinh
Lê Khánh Vinh

Reputation: 2609

Using broadcast receiver android to close app

Hi am trying to use broadcast receiver to trigger some action

  1. Activity A will broadcast action "ACTION_EXIT" and come to Main Activity.

  2. Whenever Main Activity receive the broadcast "ACTION_EXIT" it will close the app.

My code on Activity A (to send broadcast)

Intent broadcastIntent = new Intent(Beacon_MainActivity.this, MainActivity.class);
broadcastIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
broadcastIntent.setAction("com.package.ACTION_EXIT");
sendBroadcast(broadcastIntent);
finish();

Code on Main Activity to receive the broadcast and trigger ACTION_EXIT

private void registerReceiver(){
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.package.ACTION_EXIT");
        registerReceiver(myBroadcastReceiver, intentFilter);
    }

@Override
public void onResume() {
    registerReceiver();
    super.onResume();

}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(myBroadcastReceiver);
}

BroadcastReceiver myBroadcastReceiver =
        new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d("onReceive", "Logout in progress");
                Toast.makeText(MainActivity.this, "what the ****", Toast.LENGTH_SHORT).show();
                finish();
            } 
        };

Don't know why it's not working, anyone can help should be much appreciate

(app should be close when Main_Activity receive the broadcast on resume)

Upvotes: 1

Views: 1738

Answers (1)

Mike M.
Mike M.

Reputation: 39191

Your MainActivity is paused while ActivityA is running, during which time your Receiver is unregistered, and therefore not getting the broadcast. You can accomplish what you want with result forwarding instead.

In MainActivity, start LoginActivity with the startActivityForResult() method, and override the onActivityResult() method to handle the result.

public static final int REQUEST_CODE = 0;
public static final String EXTRA_EXIT = "exit";
...

Intent actLogin = new Intent(this, LoginActivity.class);
startActivityForResult(actLogin, REQUEST_CODE);
...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case REQUEST_CODE:
            if(data == null) {
                return;
            }

            boolean shouldExit = data.getBooleanExtra(EXTRA_EXIT, false);

            if(shouldExit) {
                finish();
            }
            break;
        ...
    }
}

Then, in LoginActivity, add FLAG_ACTIVITY_FORWARD_RESULT to the Intent used to start ActivityA.

Intent actA = new Intent(this, ActivityA.class);
actA.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(actA);
finish();

In ActivityA, set a boolean value to indicate whether to exit, add it to a result Intent, set that as the result, and finish the Activity.

boolean shouldExit = ...            

Intent result = new Intent();
result.putExtra(MainActivity.EXTRA_EXIT, shouldExit);
setResult(Activity.RESULT_OK, result);
finish();

The result will then be delivered back to MainActivity. This can also be done with only result codes, but I prefer using Intent extras.

Upvotes: 2

Related Questions