-3
A
回答
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();
}
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();
}
相关问题
- 1. 在QT启动应用程序时打开GPS
- 2. 如何在应用程序启动时“自动”启用GPS?
- 3. iPhone:实用程序应用程序 - 在启动时打开Flipside
- 4. 如何在每次打开应用程序时重新启动应用程序?
- 5. 在Mac应用程序中启动应用程序时,XIB未打开
- 6. 启动GPS应用程序即服务
- 7. 应用程序在后台启动时打开
- 8. 导航抽屉在应用程序启动时打开
- 9. iPad在应用程序启动时打开modalViewController
- 10. 在启动应用程序时打开网页浏览器
- 11. 启动程序,使其在启动应用程序的中心打开
- 12. 在启动时启动应用程序
- 13. 在启动时启动应用程序
- 14. 开发GPS应用程序
- 15. 启动时重新打开应用程序
- 16. VB.NET打开主exe文件时强制'启动'应用程序?
- 17. 如何在移动设备打开时在android中启动应用程序?
- 18. 当应用程序重新打开时启用启动屏幕cordova
- 19. 仅在安装并打开应用程序时才启动活动?
- 20. Firebase SDK在应用程序为后台时未打开启动器活动
- 21. 应用程序不应该启动在启动但开始
- 22. 如何在打开的应用程序上启动AlarmManager?
- 23. 关闭应用程序并在启动器上打开
- 24. 如何在设备上启动黑莓应用程序打开/打开
- 25. 打开/关闭打开CapsLock - 在启动表单应用程序期间
- 26. 如何在用户打开网址时启动我的Windows应用程序?
- 27. 如何在启动时在Cocoa应用程序中打开一个新窗口
- 28. 应用程序在打开时崩溃
- 29. 如何在拨号程序打开时启动浮动图标
- 30. 计时器在应用程序启动时自动启动
使用谷歌播放服务,您可以直接从一个对话框,而不是启用定位服务将用户发送到设置菜单,请参阅:http:// stackoverfl ow.com/questions/31235564/locationsettingsrequest-dialog-onactivityresult-skipped/31816683#31816683 –
[如何在Android中以编程方式获取当前GPS位置?](http://stackoverflow.com/questions/1513485/我该怎么做 - 我当前gps-location-program-in-android) –