【问题标题】:restrict user to disable gps option in android限制用户在android中禁用gps选项
【发布时间】:2023-12-26 12:23:01
【问题描述】:

我的应用会经常根据 GPS 监控用户位置。现在谜底是用户可以手动禁用其设备中的 GPS 选项...如何限制用户禁用 GPS 选项。?

【问题讨论】:

    标签: android gps


    【解决方案1】:

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

    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    
    boolean isGPSEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    

    如果 isGPSEnabled 为 false,则打开一个对话框,意图打开 GPS 设置

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

    同时监听 Location 监听器回调的变化。

    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
    
    }
    

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

    【讨论】:

    • 谢谢 ....是否可以在应用程序运行时限制禁用 gps 选项????
    • 不,我认为这不可能。到目前为止,我还没有找到控制安卓系统设置应用的方法。