网上搜了N多的代码,也没有个能用的。恳请大师出山哈!

解决方案 »

  1.   

    4.0 以上 不能强制打开 只能通过pendintent 打开貌似
      

  2.   

    哪个行不行呢
    public void turnGPSOn()   
    {   
         Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");   
         intent.putExtra("enabled", true);   
         this.ctx.sendBroadcast(intent);   
       
        String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);   
        if(!provider.contains("gps")){ //if gps is disabled    
            final Intent poke = new Intent();   
            poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");    
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);   
            poke.setData(Uri.parse("3"));    
            this.ctx.sendBroadcast(poke);   
        }   
    }  
      

  3.   

    package com.example.testforgps;import android.location.LocationManager;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;public class MainActivity extends Activity { Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1 = (Button)findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    //turnGPSOn();
    jumptosettings();
    }
    });
    } public void turnGPSOn()
    {
         Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
         intent.putExtra("enabled", true);
         this.sendBroadcast(intent);     String provider = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if(!provider.contains("gps")){ //if gps is disabled
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3")); 
            this.sendBroadcast(poke);
        }
    }
    public void jumptosettings()
    {
    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    //若GPS未打开,跳转到GPS设置页面
    // if (!(locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))){
    // startActivity(new Intent("android.settings.LOCATION_SOURCE_SETTINGS"));
    // }
    startActivity(new Intent("android.settings.LOCATION_SOURCE_SETTINGS"));
    }
    }另一个方法时跳到 setting 界面 需要添加 access_fine_location权限 我测试过了 都是OK的
      

  4.   

    android 2.3.1模拟器上不能打开。2.2上可以哈