简体   繁体   English

Java Android-onActivityResult在另一个活动中

[英]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. 我是Java的新手-Android,onActivityResult方法有问题,希望您能对我有所帮助。

My app using always BT adapter, but I need to check if BT adapter is available or enabled, right? 我的应用始终使用BT适配器,但是我需要检查BT适配器是否可用或启用,对吗? But this is not my problem. 但这不是我的问题。

When I'm in MainActivity class, code works fine. 当我进入MainActivity类时,代码可以正常工作。 But when I'm in another activity - onActivtiyResult (in MainActivity) not working anymore. 但是,当我处于另一个活动中时-onActivtiyResult(在MainActivity中)不再起作用。

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. 因此,当我进入MainActivity时,我会手动禁用蓝牙适配器,App要求我启用BT->当我说“不”时-应用程序退出-很好。

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. 但是,当我参加另一项活动时,我禁用了BTA,App要求我启用BTA(当我说“否”时)-不调用onActivityResult。 Why? 为什么?

So i have this code in MainActivity: 所以我在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 蓝牙方法只是父母的BluetoothAdapter ...这是我的适配器的广播

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. 在调用startActivityForResult()的活动中调用onActivityResult()。 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. 如果您已从主活动中调用startActivityForResult,则仅会调用主活动中的onActivityResult,而不会调用其他任何活动。 So adjust your code accordingly. 因此,请相应地调整代码。 .do the same in Othery activity too. 。在Othery活动中也做同样的事情。

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 . 结果的形式严格地绑定到MainActivity So, you will not get the onActivityResult called if you are in another Activity when the results arrives. 因此,如果结果到达时处于另一个Activity ,则不会调用onActivityResult

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, 每个Activity类都有一个私有实例变量,

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 BluetoothAdapter.STATE_OFF
  2. BluetoothAdapter.STATE_TURNING_OFF BluetoothAdapter.STATE_TURNING_OFF
  3. BluetoothAdapter.STATE_ON BluetoothAdapter.STATE_ON
  4. BluetoothAdapter.STATE_TURNING_ON 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. 做好登记和下面登记的所有Acvtivities你想看到蓝牙的弹出。

@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. 然后,您可以将Activity类扩展到Custom BroadCastHandleable类,并在其中拥有此私有mReciever变量,并使用该Custom Activity类扩展所有需要显示弹出窗口的Activity类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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