Intent 的 onActivityResult
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;
}
});
}
}
我想启动新意图,以便用户可以启用 GPS(如果之前未激活 GPS)。我使用 startActivityForResult 方法可以响应 Activity 是否成功。成功后,它必须对用户界面进行一些更改......我尝试启用 GPS,但 UI 上仍然没有任何更改(例如:按钮不隐藏)。我做错了什么?
@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);
}
这是我的 onActivityResult 方法。 this.finish(); 是否执行?启用后会完成当前活动,因此会启动 OnResume()?
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;
}
});
}
}
I want to start new intent so user can enable GPS (if GPS is not activated before). I used startActivityForResult method that can respond whether Activity has resulted success or not. When success it has to do some changes on the user interface....I tried and I enable the GPS but still no changes on the UI (e.g: button does not hide). What I'm doing wrong?
@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);
}
Here is my onActivityResult method. Does this.finish(); finishes the current activity when it is enabled, so it initiates OnResume()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
PreferenceActivity
与startActivityForResult()
一起使用时存在一些限制(其中一种讨论请参见 onActivityResult() 过早调用)。我的建议是:通过
startActivity()
而不是startActivityForResult()
启动PreferenceActivity
,然后放入需要重新启动的代码-读取onStart()
或onResume()
方法中的首选项,当Activity位于堆栈顶部时将立即调用该方法。There are some restrictions while using
PreferenceActivity
withstartActivityForResult()
(one of these kind of discussion see onActivityResult() called prematurely).My suggestion is: start the
PreferenceActivity
bystartActivity()
instead ofstartActivityForResult()
, and then put your code that needs to re-read the preferences inonStart()
oronResume()
method, which will be invoked immediately as the Activity is on the top of the stack.Android 中存在一个错误...将您的 Activity 更改
为
调用
startActivityForResult()
方法并再次运行。它仍然会起作用。There's a bug in Android... Change your
to
in your Activity which is calling the
startActivityForResult()
method and run again. It still will be working.