Android onActivityResult是一个意图
我想启动新的意图,以便用户可以启用GPS(如果GPS之前未激活)。我使用了startActivityForResult方法,可以响应活动是否成功。当成功时,它必须在用户界面上做一些更改…我尝试并启用了GPS,但在UI上仍然没有更改(例如:按钮不隐藏)。我做错了什么Android onActivityResult是一个意图,android,android-activity,maps,Android,Android Activity,Maps,我想启动新的意图,以便用户可以启用GPS(如果GPS之前未激活)。我使用了startActivityForResult方法,可以响应活动是否成功。当成功时,它必须在用户界面上做一些更改…我尝试并启用了GPS,但在UI上仍然没有更改(例如:按钮不隐藏)。我做错了什么 else{ //this is part of some onclick listener of a button in the program slider.close(); mapView.setC
else{ //this is part of some onclick listener of a button in the program
slider.close();
mapView.setClickable(false);
Toast.makeText(getBaseContext(), "GPS cannot retrieve your location. GPS needs clear sky or service is off. Please turn on GPS service.", Toast.LENGTH_LONG).show();
final Button bt = (Button)findViewById(R.id.turnGPS);
bt.setVisibility(View.VISIBLE);
bt.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
Intent activateGps = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//Map.this.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
Map.this.startActivityForResult(activateGps, RESULT_OK);
//bt.setVisibility(View.INVISIBLE);
return false;
}
});
}
}
这是我的onActivityResult方法。是否执行此操作。完成();启用时完成当前活动,因此它将启动OnResume() 将
首选项活动
与startActivityForResult()
一起使用时存在一些限制(此类讨论之一请参阅)
我的建议是:通过
startActivity()
而不是startActivityForResult()
启动PreferenceActivity
,然后将需要重新读取首选项的代码放入onStart()
或onResume()
方法中,当活动位于堆栈顶部时,将立即调用 Android中有一个bug。。。换衣服
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==RESULT_OK)
{
if(resultCode == RESULT_OK && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Button bt = (Button)findViewById(R.id.turnGPS);
bt.setVisibility(View.INVISIBLE);
this.finish();
}
else if(resultCode == RESULT_CANCELED)
{
Toast.makeText(getBaseContext(), "No GPS device or service enabled", Toast.LENGTH_LONG+Toast.LENGTH_LONG).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
到
在正在调用startActivityForResult()
方法并再次运行的活动中。它仍将工作。
android:launchMode="singleTop"
请考虑对答案加一个解释。
android:launchMode="singleTop"
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:alwaysRetainTaskState="True"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>