当前位置:编程学习 > wap >>

如何开启GPS?

网上搜了N多的代码,也没有个能用的。恳请大师出山哈! --------------------编程问答-------------------- 4.0 以上 不能强制打开 只能通过pendintent 打开貌似 --------------------编程问答-------------------- http://ask.csdn.net/questions/2046 --------------------编程问答-------------------- 哪个行不行呢
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);   
    }   
}  
--------------------编程问答-------------------- 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的 --------------------编程问答-------------------- android 2.3.1模拟器上不能打开。2.2上可以哈 --------------------编程问答-------------------- 这种东西 最好用真机测试 不要用模拟器
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,