如何开启GPS? 网上搜了N多的代码,也没有个能用的。恳请大师出山哈! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 4.0 以上 不能强制打开 只能通过pendintent 打开貌似 哪个行不行呢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简单app开发问题请教 求android OpenCore源代码 多个spinner级联 ,spinner中的内容需要从网络获取 求android市场的界面源码 重启tomcat后,webroot下的文件就没有了 安卓平台对讲技术相关 为什么ImageView中显示不了位图文件? 讨论下在线生成app的原理 Android WiFi热点 大家觉得这个想法能做到吗? listview 自动 删除一行 导致 getview() 重复执行 [求助贴】关于类似于微博Listview的使用方法。
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);
}
}
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的