简体   繁体   中英

Java Android - onActivityResult in another activity

I'm new in Java - Android and I have problem with onActivityResult method, I hope you can help me.

My app using always BT adapter, but I need to check if BT adapter is available or enabled, right? But this is not my problem.

When I'm in MainActivity class, code works fine. But when I'm in another activity - onActivtiyResult (in MainActivity) not working anymore.

So when I'm in MainActivity, then I disable the bluetooth adapter manually, App ask me for a enabling BT -> when i say NO - application quits - thats nice.

But, when i'm in another activity, then I disable the BTA, App ask me for a enabling the BTA (when i say NO) - onActivityResult is not called. Why?

So i have this code in MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();
        return;
    }

    registerBluetoothAdapter();
}

private void registerBluetoothAdapter()
{
    if(!bluetooth.isAvailable())
    {
        finish();
        return;
    }

    if(!bluetooth.isEnabled())
    {
        bluetooth.sendEnableRequest(this);
    }

    receiver = new BluetoothBroadcastReceiver();
    registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    switch(requestCode)
    {
        case 1: // bluetooth enable request
            if(resultCode != RESULT_OK)
            {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("EXIT", true);
                startActivity(intent);
            }
            break;
    }
}

bluetooth method is just BluetoothAdapter from parent... and here is the broadcast for my adapter

public class BluetoothBroadcastReceiver extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent)
{
    final String action = intent.getAction();

    if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED))
    {
        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
        switch (state)
        {
            case BluetoothAdapter.STATE_OFF:
                MainActivity main = (MainActivity) context;
                main.startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1);
                break;
        }
    }
}

}

I have bad feelings of my english, so.. sorry about that and thank you for help.

onActivityResult() is called in the activity from where the startActivityForResult() is called. If you've called startActivityForResult from main activity then only the onActivityResult that is in the main activity will be called back not any other activity. So adjust your code accordingly. .do the same in Othery activity too.

Ok, You are want to get what is happening on the Bluetooth setting page from any Activity. Following is the closest you can get (I think).

If you do this,

MainActivity main = (MainActivity) context;
                main.startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1);

the result form this is strictly bound to the MainActivity . So, you will not get the onActivityResult called if you are in another Activity when the results arrives.

Add the permission,

<uses-permission android:name="android.permission.BLUETOOTH" />

and

<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />

Have a private instance variable of your every Activity class,

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                                                 BluetoothAdapter.ERROR);
            switch (state) {
             //Handle your case
            }
        }
    }
};

There are different cases,

  1. BluetoothAdapter.STATE_OFF
  2. BluetoothAdapter.STATE_TURNING_OFF
  3. BluetoothAdapter.STATE_ON
  4. BluetoothAdapter.STATE_TURNING_ON

And,

Do the registration and registration as below in all the Acvtivities you would like to see the Bluetooth a popup.

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

    // Register for reciever
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(mReceiver, filter);
}

@Override
public void onDestroy() {
    super.onDestroy();

    // Unregister broadcast listeners
    unregisterReceiver(mReceiver);
}

Then manage the exit of the application from each of the Activity.

Note:

If you want to reuse the code,

Then you can extend the Activity class to your Custom BroadCastHandleable class and have this private mReciever variable in there and Extend all your Activity classes that need to display the popup with this custom Activity class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM