繁体   English   中英

Android如何在按返回键回到主屏幕后关闭gps图标?

[英]Android how to turn off gps icon after press return back button to home screen ?

我有这个问题,当我想返回首页时,我从应用程序中使用GPS来获取经度和纬度。图标GPS总是从移动设备的顶部显示,返回主屏幕后如何关闭它

在此处输入图片说明 我的代码。

public class doctorlocation extends Activity implements LocationListener {
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;
    String lat;
    String provider;
    protected double latitude,longitude; 
    protected boolean gps_enabled,network_enabled;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.doctorlocation);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);   
    }

        @Override
    public void onLocationChanged(Location location) {
     //txtLat = (TextView) findViewById(R.id.textview1);

    //txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
    String str = "Latitude: "+location.getLatitude()+" \nLongitude: "+location.getLongitude();
    Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();

    latitude=location.getLatitude();

    longitude=location.getLongitude();

    }

    @Override
    public void onProviderDisabled(String provider) {
     Log.d("Latitude","disable");

        Toast.makeText(getBaseContext(), "Gps turned off ", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onProviderEnabled(String provider) {
     Log.d("Latitude","enable");

    Toast.makeText(getBaseContext(), "Gps turned on ", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("Latitude","status");
    }

      }

在该Activity onPause()上使用此代码。

 @Override
    protected void onPause() {
    Log.i("onPause", "inside onPause");
    super.onPause();
    locationManager.removeUpdates(myLocationListener);//locationManager.removeUpdates(this) ;
    locationManager = null;
    }

启用和禁用GPS掌握在用户手中。 您可以向他显示一个对话框,通知他有关禁用GPS的信息。 在这种情况下,请在对话框上保留两个按钮-一个用于“设置”,另一个用于“确定”或“取消”。

public static void promptForGPS(
        final Activity activity)
    {
        final AlertDialog.Builder builder =
            new AlertDialog.Builder(activity);
        final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
        final String message = "Disable GPS....Message here"
            + " Click OK to go to"
            + " location services settings to let you do so.";

        builder.setMessage(message)
            .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface d, int id) {
                        activity.startActivity(new Intent(action));
                        d.dismiss();
                    }
            })
            .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface d, int id) {
                        d.cancel();
                    }
            });
        builder.create().show();
    }    

我发现的最好的是

onResume()
{
     // Request for GPS Location
     //location.requestLocationUpdates()
}

onPause()
{
     // Remove location
     // location.removeUpdates()
}

通过调用removeUpdates()方法,只要您的活动不在地面上,它将停止GPS。 这样,您既可以停止GPS,也可以停止消耗电池电量。

您需要通过使用以下命令告知LocationManager,当您的应用进入后台时,您不再需要位置更新

locationManager.removeUpdates(this);

在您的onPause()方法中。

您还应该将呼叫移至

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);   

到onResume(); 这样,一旦您重新输入应用,位置更新就会恢复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM