2015-10-07 84 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(); 
    } 
相关问题