我正在gpsprovider上工作,如果用戶點擊基於gps的應用程序假設地圖應用程序,它檢查gps提供程序是否啓用,如果不提醒用戶啓用gpsprovider。如何通知用戶啓用gpsprovider
將會有一個廣播接收器。 將定義從任何應用程序請求服務的消息。如果未啓動,將啓動服務。 將定義一條消息,指出應用程序不再需要它。如果沒有應用程序需要它將停止服務
我正在gpsprovider上工作,如果用戶點擊基於gps的應用程序假設地圖應用程序,它檢查gps提供程序是否啓用,如果不提醒用戶啓用gpsprovider。如何通知用戶啓用gpsprovider
將會有一個廣播接收器。 將定義從任何應用程序請求服務的消息。如果未啓動,將啓動服務。 將定義一條消息,指出應用程序不再需要它。如果沒有應用程序需要它將停止服務
AFAIK它仍然不可能以編程方式啓動GPS服務。你能做的唯一的事情就是打開設置頁面爲用戶改變設置他們的自我:
if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
{
Intent myIntent = new Intent(Settings.ACTION_SECURITY_SETTINGS); startActivity(myIntent);
}
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
}
}
在你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());
}
}
Scoobler嗨,感謝您的寶貴信息 – dhriti 2010-11-06 13:33:11
也不應該是可能的應用程序以編程方式啓用該選項:可能存在嚴重的安全和隱私風險,用戶。 – Richard 2016-03-19 18:43:35