android2.2 
locationDemo现在有多个activity,实际手机使用退出的时候发现 手机的gps总是亮着 关不到高德地图咩有发现这个问题  (如何能彻底退出程序
关闭所有acitiviy  自动关闭程序监听)核心代码如下 (销毁采用 finish());package ziv.locationdemo;import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class LocationDemo extends Activity {
 TextView locationShow = null;        // 显示区
 TextView infoTextView = null;        // 提示信息显示
 Button locationbutton = null;        // 绑定GPS监听按钮
 Button locationbuttonNetwork=null;       // 绑定NEWWOR简体按钮
 LocationManager locationManager = null;     // 定位管理
 public LocationListener locationListener_GPS = null;  // 监听gps返回数据
 public LocationListener locationListener_NETWORK=null;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  // 实例化显示区
  locationShow = (TextView) findViewById(R.id.locationShow);
  // 在视图层提出提示信息文本框
  infoTextView = (TextView) findViewById(R.id.infoTextView);
  // 实例化绑定监听按钮
  locationbutton = (Button) findViewById(R.id.locationbutton);
  locationbuttonNetwork = (Button) findViewById(R.id.locationbuttonNetwork);
  
  // 获得定位管理类
  locationManager = (LocationManager) LocationDemo.this
    .getSystemService(Context.LOCATION_SERVICE);
  // 实例化监听对象变量
  locationListener_GPS = new LocationListenerSelf("GPS");
  locationListener_NETWORK=new LocationListenerSelf("NETWORK");
  locationbuttonNetwork.setOnClickListener(new OnClickListenerButton(LocationManager.NETWORK_PROVIDER));
  // 监听单击按钮
  locationbutton.setOnClickListener(new OnClickListenerButton(LocationManager.GPS_PROVIDER)); } // 执行就绪
 @Override
 protected void onResume() {
  // 在就绪执行中,自动选择位置信息提供者,且触发监听
  if (locationManager != null) {   // 实例化查询条件
   Criteria criteria = new Criteria();
   criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度
   // 获得定位服务提供者名称
   String provider = locationManager.getBestProvider(criteria, true);
   
   //locationManager.setTestProviderEnabled("gps", true);
   //criteria.setCostAllowed(true);   // 自动位置变化监听
   if("gps".equals(provider)){
    
    locationManager.requestLocationUpdates(provider, 2000, 0,
      locationListener_GPS);
   }else{
    locationManager.requestLocationUpdates(provider, 2000, 0,
      locationListener_NETWORK);
    
   }
    
   // 获得最后的位置信息
   Location location = locationManager.getLastKnownLocation(provider);
   // 显示坐标信息
   if (location != null) {
    showLocation(location,provider);
   } else {
    locationShow.append(" onResume 没获得到定位\n");
    infoTextView.setText("提供者:"+provider);
   }
  }
  super.onResume();
 } /**
  * 显示location 信息
  * 
  * @param location
  */
 private void showLocation(Location location,String provider) {  locationShow.append("经度:" + location.getLongitude() + "\n" + "纬度:"
    + location.getLatitude() + "\n");
  infoTextView.setText("提供者:"+provider); }
 
 @Override
 protected void onPause() {
  System.out.println("onPause");
  if (locationManager != null) {
   locationManager.removeUpdates(locationListener_GPS);
   locationManager.removeUpdates(locationListener_NETWORK);
  }
  super.onPause();
 }
 private class OnClickListenerButton implements OnClickListener{
  String provider="";
  public OnClickListenerButton(){
   super();
  }
  public OnClickListenerButton(String provider){
   super();
   this.provider=provider;
  }
  @Override
  public void onClick(View v) {
 
   Location location = locationManager
     .getLastKnownLocation(provider);
   // 显示坐标信息
   if (location != null) {
    showLocation(location,provider);
   } else {
    locationShow.append(" 没获得到定位\n");
    infoTextView.setText("提供者:"+provider+";是否可用:"+locationManager.isProviderEnabled(provider));
   }
   //启动gps监听
   if(locationManager.GPS_PROVIDER.equals(provider)){
    
    locationManager.requestLocationUpdates(
      provider, 1000, 0,
      locationListener_GPS);
   }else{
    locationManager.requestLocationUpdates(
      provider, 1000, 0,
      locationListener_NETWORK);
   }
   
  } 
 }
 /**
  * 位置变化监听
  * @author ziv
  *
  */
 private class LocationListenerSelf implements LocationListener{
  String provider="null";
  public LocationListenerSelf(){
   
  }
  public LocationListenerSelf(String provider){
   this.provider=provider;
  }
  @Override
  public void onLocationChanged(Location location) {
   if (location != null) {
    showLocation(location,provider);
   } else {
    locationShow.append(" location listener 没获得到定位 \n");
    infoTextView.setText("提供者:"+provider);
   }
  }  @Override
  public void onProviderDisabled(String provider) {
   System.out.println("onProviderDisabled");  }  @Override
  public void onProviderEnabled(String provider) {
   System.out.println("onProviderEnabled");  }  @Override
  public void onStatusChanged(String provider, int status,
    Bundle extras) {
   System.out.println("onStatusChanged");  } 
 }
}

解决方案 »

  1.   

    在关闭的地方写个广播,通知每个Activity关闭
      

  2.   

    我是这样 做 baseacitity基础类 
    acitiviy1  -------startactity(1)  
    acitiviy2  -------startactity(1)
    acitiviy3  -------startactity(1) 
    全部用的这个(用广播可以) 
    现在是gps 的 那个标志始终在手机上,似乎没有彻底关闭 (高德地图可以彻底关闭gps)请看上面的代码 ,估计gps没有彻底 finish()   (需要代码里加一些控制gps监听关闭)
      

  3.   

    http://blog.sina.com.cn/s/blog_90cdca4c01016gzt.html
    http://www.cnblogs.com/guige/archive/2012/08/06/2625249.html
    public class Deaboway extends Application
    {     
    private List<Activity> mainActivity = new ArrayList<Activity>();   
    public List<Activity> MainActivity()
    {           return mainActivity;       }     
    public void addActivity(Activity act)
    {           mainActivity.add(act);       }
    public void finishAll() {         
    for (Activity act : mainActivity) { 
    if (!act.isFinishing()) {    
    act.finish();            
    }        
    }        
    mainActivity = null;    

    }  Deaboway appState = (Deaboway)this.getApplication();
      appState.addActivity(this); 
      Deaboway appState1 = (Deaboway)getApplicationContext();
      
      
      
      在使用的地方添加如下代码  appState1.finishAll();