我正在研究gpsprovider,如果用户点击基于gps的应用程序来假设地图应用程序,它会检查是否启用了gps提供程序,如果没有警告用户启用gpsprovider。
将有一个广播接收者。 将定义从任何应用程序请求服务的消息。如果没有启动,将启动服务。 将定义一条消息,说明应用程序不再需要它。如果没有应用程序需要它将停止服务
答案 0 :(得分:4)
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider != null ){
if(! provider.contains("gps")){
// Notify users and show settings if they want to enable GPS
new AlertDialog.Builder(MessagePage.this)
.setMessage("GPS is switched off. enable?")
.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 5);
}
})
.setNegativeButton("Don't do it", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 5 && resultCode == 0){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider != null){
switch(provider.length()){
case 0:
//GPS still not enabled..
break;
default:
Toast.makeText(this, "GPS is now enabled.", Toast.LENGTH_LONG).show();
break;
}
}
}
else{
//the user did not enable his GPS
}
}
答案 1 :(得分:1)
AFAIK仍无法以编程方式启动GPS服务。您唯一能做的就是打开设置页面,让用户自行更改设置:
if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ))
{
Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS ); startActivity(myIntent);
}
答案 2 :(得分:0)
在您的CommonUtils.java
中public class CommonUtils {
public static void displayPromptForEnablingGPS(
final Activity activity)
{
final AlertDialog.Builder builder =
new AlertDialog.Builder(activity);
final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
final String message = "Enable either GPS or any other location"
+ " service to find current location. Click OK to go to"
+ " location services settings to let you do so.";
builder.setMessage(message)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int id) {
activity.startActivity(new Intent(action));
d.dismiss();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int id) {
d.cancel();
}
});
builder.create().show();
}
}
在您的活动中
private void getGPSInfo() {
LocationManager locationmanager = (LocationManager) getActivity()
.getSystemService(Context.LOCATION_SERVICE);
if (locationmanager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// YOUR MAPS ACTIVITY CALLING or WHAT YOU NEED
} else {
CommonUtils.displayPromptForEnablingGPS(getActivity());
}
}