首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >限制用户在android中禁用gps选项

限制用户在android中禁用gps选项
EN

Stack Overflow用户
提问于 2013-02-21 21:41:14
回答 1查看 1.6K关注 0票数 0

我的应用程序将经常监测用户的位置基于GPS..now的谜语是用户可以手动禁用他们的device...how中的全球定位系统选项,以限制用户禁用全球定位系统选项。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-21 21:49:19

您可以检查GPS是否被禁用。

代码语言:javascript
复制
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

boolean isGPSEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

然后,如果isGPSEnabled为false,则打开一个对话框,以便打开gps设置

代码语言:javascript
复制
private void showSettingsGPSAlert() {

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

    // Setting Dialog Title
    alertDialog.setTitle("GPS settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();

}

还要监听位置监听程序回调中的更改。

代码语言:javascript
复制
public void onProviderDisabled(String provider) {

         //Do something here. Example tell user that gps is disabled

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

我希望这些对你有所帮助,祝你编码愉快!:)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15003756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档