将onReceive置于等待,直到开始的活动结束

时间:2017-07-04 11:53:05

标签: android android-intent android-broadcastreceiver

在onReceive()中,我想创建一个新的Intent并启动一个新的Activity。活动结束后,我想回到我离开活动的同一行。我知道我不能使用BroadcastReceiver类来实现这一目标。怎么能实现这一目标?下面是onReceive()代码:

final BroadcastReceiver mPairingRequestRecevier = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(intent.getAction()))
        {
            final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);

            if (type == BluetoothDevice.PAIRING_VARIANT_PIN)
            {
                abortBroadcast();
                String passkey="hey";
                Intent passkeyIntent = new Intent(context.getApplicationContext(),TestActivity.class);
                passkeyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                passkeyIntent.putExtra("passkey",passkey);
                context.startActivity(passkeyIntent);
                Log.d("after returning...",passkey);

            }
            else
            {
                Log.d("Unexpected pairingtype:" , type+"");
            }
        }
    }
};

任何可以实现这一目标的想法都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

您可以使用startActivityFor结果而不是startActivity来启动活动。使其执行某项任务并将结果返回到上一个活动 看看这个:How to manage `startActivityForResult` on Android?