2015-10-07 62 views
-3

我已經開始使用Android中的GoogleMap,我希望我的應用程序在應用程序啓動時自動打開GPS,並在應用程序關閉或用戶退出應用程序時關閉。在應用程序啓動時打開GPS

如何完成上述任務?

+2

使用谷歌播放服務,您可以直接從一個對話框,而不是啓用定位服務將用戶發送到設置菜單,請參閱:http:// stackoverfl ow.com/questions/31235564/locationsettingsrequest-dialog-onactivityresult-skipped/31816683#31816683 –

+0

[如何在Android中以編程方式獲取當前GPS位置?](http://stackoverflow.com/questions/1513485/我該怎麼做 - 我當前gps-location-program-in-android) –

回答

2

GPS真的不能自動被一個Android應用程序打開。它需要用戶的身份驗證才能這樣做。

最好你可以做的是有一個彈出式對話框,要求用戶允許訪問位置服務。

3

,您可以提示用戶打開GPS,如:

private void buildAlertMessageNoGps() { 
     final AlertDialog.Builder builder = new AlertDialog.Builder(
       this.getActivity()); 
     builder.setMessage(R.string.gps_disabled) 
       .setCancelable(false) 
       .setTitle(R.string.gps_disabled_title) 
       .setPositiveButton(R.string.enable, 
         new DialogInterface.OnClickListener() { 
          public void onClick(
            @SuppressWarnings("unused") final DialogInterface dialog, 
            @SuppressWarnings("unused") final int id) { 
           startActivity(new Intent(
             android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
          } 
         }) 
       .setNegativeButton(R.string.cancel, 
         new DialogInterface.OnClickListener() { 
          public void onClick(final DialogInterface dialog, 
            @SuppressWarnings("unused") final int id) { 
           dialog.cancel(); 

          } 
         }); 
     final AlertDialog alert = builder.create(); 
     alert.show(); 
    } 
+0

這是否仍然有效?你測試過了嗎? –

+1

現在這將工作 – Jas

+1

這是打開GPS的按鈕 – Jas

1

不是不可能的,也不適合。你不能在沒有他的權力的情況下管理用戶的電話。

從Play商店:

「地獄犬自動啓用GPS,如果當您嘗試 定位設備(僅適用於Android 2.3.3 <),你可以保護它 免受未經授權的卸載它關閉 - 更多信息在應用程序配置中。「

你可以做這樣的事情:

startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
0

完整的解決方案是在這裏。

AndroidManifest.xml中

也許你需要如下因素權限

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

MainActivity代碼

public class MainActivity extends AppCompatActivity { 

Context context; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //... some code to init Activity and etc... 

     context = this; 

     LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

     if(!statusOfGPS) // Before show message to turn on GPS be sure it is turned off. 
     { 
      buildAlertMessageNoGps(); 
     } 
    } 



private void buildAlertMessageNoGps() { 
     final AlertDialog.Builder builder = new AlertDialog.Builder(
       this.getActivity()); 
     builder.setMessage(R.string.gps_disabled) 
       .setCancelable(false) 
       .setTitle(R.string.gps_disabled_title) 
       .setPositiveButton(R.string.enable, 
         new DialogInterface.OnClickListener() { 
          public void onClick(
            @SuppressWarnings("unused") final DialogInterface dialog, 
            @SuppressWarnings("unused") final int id) { 
           startActivity(new Intent(
             android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
          } 
         }) 
       .setNegativeButton(R.string.cancel, 
         new DialogInterface.OnClickListener() { 
          public void onClick(final DialogInterface dialog, 
            @SuppressWarnings("unused") final int id) { 
           dialog.cancel(); 

          } 
         }); 
     final AlertDialog alert = builder.create(); 
     alert.show(); 
    } 
相關問題