当GPS通过打开GPS设置而启用时,我希望能够刷新我的活动。我实现了onResume函数,但是结果在我的logcat上看到了这个:
performing stop of activity that is not resumed(...)
我查看了这个帖子,但我无法弄清楚我的代码(或我的onResume函数)有什么问题,并且如何在从GPS设置返回后刷新我的活动。
请问您能帮我吗?这是我的MainActivity.java。
当GPS通过打开GPS设置而启用时,我希望能够刷新我的活动。我实现了onResume函数,但是结果在我的logcat上看到了这个:
performing stop of activity that is not resumed(...)
gps.showSettingsAlert();//inside this use startActivityForResult(new Intent().setClass(this, MainActivity.class), result_code);
编辑:你的方法应该在MainActivity.class中而不是在Service中,因为Service没有UI,所以在那里获取结果没有意义。
public void showSettingsAlert(Context context){
AlertDialog.Builder alertdialog = new AlertDialog.Builder(context);
alertdialog.setTitle("Oups ! pas de données GPS");
alertdialog.setMessage("GPS n'est pas activé sur votre appareil, voulez vous l'activer ?");
/*
handling the buttons :
*/
alertdialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, RESULT_CODE_GPS);
}
});
// on pressing cancel button
alertdialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertdialog.show();
}
@SuppressLint("NewApi") @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == RESULT_CODE_GPS && resultCode == 0){
// @SuppressWarnings("deprecation")
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_MODE);
if(provider != null && !provider.isEmpty()){
Log.v("mAINaBBB", " Location providers: "+provider);
//Start searching for location and update the location text when update available.
// Do whatever you want
}else{
Log.v(, "Nothing put on");
//Users did not switch on the GPS
}
}
}
if(gps.canGetlocation() ){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
device_location = new ParseGeoPoint(latitude,longitude);
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
showSettingsAlert(MainActivity.this); //**This line should be changed then**
}
@Override
protected void onResume() {
super.onResume();
Intent refresh =new Intent(this, MainActivity.class);
startActivity(refresh);
}
这只是一个糟糕的想法,会导致无限循环,其中MainActivity不断启动新的MainActivity。
只需删除您的onResume()并刷新当前活动的视图,而不是创建新的活动。