Java Android - onActivityResult 在另一个 activity
Java Android - onActivityResult in another activity
我是 Java - Android 的新手,我对 onActivityResult 方法有疑问,希望你能帮助我。
我的应用程序一直使用 BT 适配器,但我需要检查 BT 适配器是否可用或启用,对吗?但这不是我的问题。
当我在 MainActivity class 中时,代码工作正常。但是当我在另一个 activity 时 - onActivtiyResult(在 MainActivity 中)不再工作。
因此,当我在 MainActivity 中时,我手动禁用了蓝牙适配器,应用程序要求我启用 BT -> 当我说“不”时 - 应用程序退出 - 这很好。
但是,当我在另一个 activity 中时,我禁用了 BTA,应用程序要求我启用 BTA(当我拒绝时)- 未调用 onActivityResult。为什么?
所以我在 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;
}
}
蓝牙方法只是来自父级的 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;
}
}
}
}
我的英语不好,所以..抱歉,谢谢你的帮助。
好的,您想要从任何 Activity 获取蓝牙设置页面上发生的事情。以下是你能得到的最接近的(我认为)。
如果你这样做,
MainActivity main = (MainActivity) context;
main.startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1);
结果形式严格绑定到 MainActivity
。因此,如果结果到达时您在另一个 Activity
中,则不会调用 onActivityResult
。
添加权限,
<uses-permission android:name="android.permission.BLUETOOTH" />
和
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
每个 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
}
}
}
};
有不同的情况,
- BluetoothAdapter.STATE_OFF
- BluetoothAdapter.STATE_TURNING_OFF
- BluetoothAdapter.STATE_ON
- BluetoothAdapter.STATE_TURNING_ON
并且,
在所有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);
}
然后从每个 Activity.
管理应用程序的退出
注:
如果您想重复使用代码,
然后你可以将 Activity
class 扩展到你的自定义 BroadCastHandleable
class 并在那里有这个私有 mReciever 变量并扩展你所有的 Activity class需要使用此自定义显示弹出窗口的元素 Activity class。
onActivityResult() 在调用 startActivityForResult() 的 activity 中被调用。如果您从 main activity 调用了 startActivityForResult,那么只有 main activity 中的 onActivityResult 会被回调,而不会回调任何其他 activity。所以相应地调整你的代码。 .在 Othery activity 中也做同样的事情。
我是 Java - Android 的新手,我对 onActivityResult 方法有疑问,希望你能帮助我。
我的应用程序一直使用 BT 适配器,但我需要检查 BT 适配器是否可用或启用,对吗?但这不是我的问题。
当我在 MainActivity class 中时,代码工作正常。但是当我在另一个 activity 时 - onActivtiyResult(在 MainActivity 中)不再工作。
因此,当我在 MainActivity 中时,我手动禁用了蓝牙适配器,应用程序要求我启用 BT -> 当我说“不”时 - 应用程序退出 - 这很好。
但是,当我在另一个 activity 中时,我禁用了 BTA,应用程序要求我启用 BTA(当我拒绝时)- 未调用 onActivityResult。为什么?
所以我在 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;
}
}
蓝牙方法只是来自父级的 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;
}
}
}
}
我的英语不好,所以..抱歉,谢谢你的帮助。
好的,您想要从任何 Activity 获取蓝牙设置页面上发生的事情。以下是你能得到的最接近的(我认为)。
如果你这样做,
MainActivity main = (MainActivity) context;
main.startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1);
结果形式严格绑定到 MainActivity
。因此,如果结果到达时您在另一个 Activity
中,则不会调用 onActivityResult
。
添加权限,
<uses-permission android:name="android.permission.BLUETOOTH" />
和
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
每个 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
}
}
}
};
有不同的情况,
- BluetoothAdapter.STATE_OFF
- BluetoothAdapter.STATE_TURNING_OFF
- BluetoothAdapter.STATE_ON
- BluetoothAdapter.STATE_TURNING_ON
并且,
在所有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);
}
然后从每个 Activity.
管理应用程序的退出注:
如果您想重复使用代码,
然后你可以将 Activity
class 扩展到你的自定义 BroadCastHandleable
class 并在那里有这个私有 mReciever 变量并扩展你所有的 Activity class需要使用此自定义显示弹出窗口的元素 Activity class。
onActivityResult() 在调用 startActivityForResult() 的 activity 中被调用。如果您从 main activity 调用了 startActivityForResult,那么只有 main activity 中的 onActivityResult 会被回调,而不会回调任何其他 activity。所以相应地调整你的代码。 .在 Othery activity 中也做同样的事情。