我正在开发一个应用程序,其中我想启用GPS,当我开始我的活动时,我想在退出应用程序时禁用GPS。我可以通过我的程序来完成..
任何帮助都将不胜感激。
答案 0 :(得分:6)
这是代码......
private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){
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"));
sendBroadcast(poke);
} }
答案 1 :(得分:6)
您可以使用
启动GPSLocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new CTLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, locationListener);
停止使用gps提供程序
locationManager.removeUpdates(locationListener);
答案 2 :(得分:2)
我在所有网站上研究过这个主题。我发现这个代码如下。我尝试了它的工作。
方法-1
Enable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
Disable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
方法-2
// If GPS is OFF
private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){
final Intent intent = new Intent();
intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
intent.setData(Uri.parse("3"));
sendBroadcast(intent);
}
}
// If GPS is ON
private void turnGPSOff(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){
final Intent intent = new Intent();
intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
intent.setData(Uri.parse("3"));
sendBroadcast(intent);
}
}
您可以访问此网站了解更多信息:http://androidmaterial.blogspot.com/2012/04/how-to-enabledisable-gps-in-android.html
答案 3 :(得分:0)
以下是我能找到的所有内容(我的代码不会在一个活动中运行,因此我有一个ApplicationContext类,我称之为静态方法getInstance。您不必在活动中执行此操作。抱歉代码格式化):
public static boolean isGPSEnabled(){
LocationManager locationManager = (LocationManager)ApplicationContext.getInstance().getSystemService(Context.LOCATION_SERVICE);
boolean enabled = locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER );
if(enabled) return enabled;
String provider = Settings.Secure.getString(ApplicationContext.getInstance().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return provider.contains("gps");
}
private static boolean canToggleGPSDirectly() {
PackageManager pacman = ApplicationContext.getInstance().getPackageManager();
PackageInfo pacInfo = null;
try {
pacInfo = pacman.getPackageInfo("com.android.settings", PackageManager.GET_RECEIVERS);
} catch (NameNotFoundException e) {
return false; //package not found
}
if(pacInfo != null){
for(ActivityInfo actInfo : pacInfo.receivers){
//test if recevier is exported. if so, we can toggle GPS.
if(actInfo.name.equals("com.android.settings.widget.SettingsAppWidgetProvider") && actInfo.exported){
return true;
}
}
}
return false; //default
}
//this only works in older versions of android (it woks on 2.2)
private static void toggleGPS(){
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"));
ApplicationContext.getInstance().sendBroadcast(poke);
}
private static void showGPSSettingsScreen(){
//show the settings screen
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ApplicationContext.getInstance().startActivity(myIntent);
// this doens't work if your app isn't signed as firmware....Settings.Secure.setLocationProviderEnabled(ApplicationContext.getInstance().getContentResolver(), LocationManager.GPS_PROVIDER, true);
}
public static void ensureGPSStatus(boolean on){
boolean isGPSEnabled = isGPSEnabled();
if((isGPSEnabled && !on) || (!isGPSEnabled && on)){
if(canToggleGPSDirectly()){
toggleGPS();
}else{
showGPSSettingsScreen();
}
}
}
答案 4 :(得分:0)
要通过代码禁用GPS,
Intent my_intent = new Intent();
my_intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
my_intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
my_intent.setData(Uri.parse("3"));
sendBroadcast(my_intent); //use context.sendBroadcast(my_intent) if you are in any Broadcast activity
答案 5 :(得分:0)
如果我没有记错,你不能完全以编程方式完成。最好是触发启动设置页面的意图,但随后用户必须在设置页面中启用gps设置。
如果没有用户干预,您无法在后台执行此操作。
答案 6 :(得分:0)
我知道为时已晚,但直接启动位置服务将无效。而是使用下面的代码来启动位置服务。
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
答案 7 :(得分:0)
<强> GpsSettings 强>
public class GpsSettings
{
@SuppressWarnings("deprecation")
public static void turnGPSOn(Context context)
{
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
context.sendBroadcast(intent);
String provider = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Settings.Secure.setLocationProviderEnabled(context.getContentResolver(), "gps", true);
if (!provider.contains("gps"))
{
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"));
context.sendBroadcast(poke);
}
}
@SuppressWarnings("deprecation")
public static void turnGPSOff(Context context)
{
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
context.sendBroadcast(intent);
String provider = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (provider.contains("gps"))
{
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"));
context.sendBroadcast(poke);
}
}
}