2010-10-27 25 views
2

我正在gpsprovider上工作,如果用戶點擊基於gps的應用程序假設地圖應用程序,它檢查gps提供程序是否啓用,如果不提醒用戶啓用gpsprovider。如何通知用戶啓用gpsprovider

將會有一個廣播接收器。 將定義從任何應用程序請求服務的消息。如果未啓動,將啓動服務。 將定義一條消息,指出應用程序不再需要它。如果沒有應用程序需要它將停止服務

回答

1

AFAIK它仍然不可能以編程方式啓動GPS服務。你能做的唯一的事情就是打開設置頁面爲用戶改變設置他們的自我:

if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) 
{ 
    Intent myIntent = new Intent(Settings.ACTION_SECURITY_SETTINGS); startActivity(myIntent); 
} 
+0

Scoobler嗨,感謝您的寶貴信息 – dhriti 2010-11-06 13:33:11

+0

也不應該是可能的應用程序以編程方式啓用該選項:可能存在嚴重的安全和隱私風險,用戶。 – Richard 2016-03-19 18:43:35

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 
     } 
} 
+0

嗨Umesh,感謝分享代碼 – dhriti 2010-11-06 14:16:19

+0

其實我能夠在應用程序內啓用gps。但是,我正在尋找解決方案,可以啓用gps或在使用任何基於gps的應用程序時提醒用戶。 – dhriti 2010-11-06 14:25:57

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()); 
    } 
}