0

如果用户不批准激活蓝牙,我有一个不应打开的片段,这是通过这段代码请求的

    Intent mIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        mIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 240);
            startActivityForResult(mIntent, 1);
    //Here I want to exit the method in case result is denied
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content,new ServerFragment()).addToBackStack(null).commit();

我一直在寻找很多地方,我发现的唯一一件事是使用新活动中的 setResult (在我的情况下, onActivityResult 在调用活动中被覆盖,因为此代码位于片段中)

有任何想法吗?

谢谢!

编辑:这是一个堆栈跟踪

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131073, result=240, data=null} to activity {bt.bt/bt.bt.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
                                                                               at android.app.ActivityThread.deliverResults(ActivityThread.java:3659)
                                                                               at android.app.ActivityThread.handleSendResult(ActivityThread.java:3702)
                                                                               at android.app.ActivityThread.access$1300(ActivityThread.java:155)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:135)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                                            Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
4

3 回答 3

0

我认为您应该使用 BroadcastReceiver 来注册事件蓝牙。在您的 Manifest.xml 中,您需要添加两个语句:

<uses-permission android:name="android.permission.BLUETOOTH" />
<receiver android:name=".BluetoothBroadcastReceiver"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
 </intent-filter>
    </receiver>

然后你需要实现 onReceive() 方法:

public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    Log.d("BroadcastActions", "Action "+action+"received");
    int state;
    BluetoothDevice bluetoothDevice;

    switch(action)
    {
        case BluetoothAdapter.ACTION_STATE_CHANGED:
            state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
            if (state == BluetoothAdapter.STATE_OFF)
            {
                Toast.makeText(context, "Bluetooth is off", Toast.LENGTH_SHORT).show();
                Log.d("BroadcastActions", "Bluetooth is off");
            }
            else if (state == BluetoothAdapter.STATE_TURNING_OFF)
            {
                Toast.makeText(context, "Bluetooth is turning off", Toast.LENGTH_SHORT).show();
                Log.d("BroadcastActions", "Bluetooth is turning off");
            }
            else if(state == BluetoothAdapter.STATE_ON)
            {
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content,new ServerFragment()).addToBackStack(null).commit();
                Log.d("BroadcastActions", "Bluetooth is on");
            }
            break;

....
}

您可以在此链接中查看更多信息:Android Broadcast Receiver 蓝牙事件捕获

于 2016-03-04T01:03:38.380 回答
0

您应该在启动意图后返回您的函数。然后等待 onActivityResult 中的回调。在这种方法中,如果结果是 RESULT_OK,那么您将开始您的片段。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == your_request_code_here) {
        if (resultCode != Activity.RESULT_CANCELED) {
            // start fragment
        }
    }
}

完整活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent mIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                mIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 240);
                startActivityForResult(mIntent, 1);
            }
        });
    }

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

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

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (resultCode != Activity.RESULT_CANCELED) {

            }
        }
    }
}
于 2016-03-04T00:37:09.510 回答
0

正如您粘贴的代码一样,您一调用就实例化了片段startActivityForResult(),您不需要这样做 - 这就是导致问题的原因。您只需要startActivityForResult()然后在您onActivityResult()的活动中的方法中,如果 resultCode 是,您可以实例化您的片段RESULT_OK

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == your_request_code_here) {
        if (resultCode == Activity.RESULT_OK) {

getSupportFragmentManager().beginTransaction().replace(R.id.content,new ServerFragment()).addToBackStack(null).commit();
        }
    }
}
于 2016-03-04T00:50:35.680 回答