【问题标题】:Listen to location access disable/enable in settings在设置中收听位置访问禁用/启用
【发布时间】:2013-11-28 12:16:07
【问题描述】:

在 Android 操作系统中,在“设置” --> “定位服务”中,有一个名为“访问我的位置的切换按钮>" 可用于禁用和启用来自应用的位置信息访问。

目前,我正在开发一个定位服务应用程序。我想知道,如何在我的 Android 项目中收听此设置?当用户禁用或启用“访问我的位置”时,我可以使用任何广播接收器立即知道吗?

如果没有任何广播接收器,我如何在我的 Android 项目中收听这种变化?

【问题讨论】:

标签: android android-intent broadcastreceiver android-broadcast


【解决方案1】:

这对我有用:

将接收者添加到 Manifest 文件中:

<receiver
        android:name="com.eegeo.location.LocationProviderChangedReceiver">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
        </intent-filter>
    </receiver>

检查接收器中的两个位置提供程序:

public class LocationProviderChangedReceiver  extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        boolean anyLocationProv = false;
        LocationManager locationManager = (LocationManager) MyMainActivity.context.getSystemService(Context.LOCATION_SERVICE);

        anyLocationProv |= locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        anyLocationProv |=  locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        Log.i("", "Location service status" + anyLocationProv);


    }

}

虽然由于显而易见的原因多次调用此接收器,但这会告诉您状态。

【讨论】:

【解决方案2】:

您可以通过这种方式简单得多。在您的 BroadcastReceiver 的 onReceive() 方法中:

    ContentResolver contentResolver = getContext().getContentResolver();
    // Find out what the settings say about which providers are enabled
    int mode = Settings.Secure.getInt(
            contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);

    if (mode == Settings.Secure.LOCATION_MODE_OFF) {
        // Location is turned OFF!
    } else {
        // Location is turned ON!
    }

~感谢这个代码:LocationManagerTest.java

【讨论】:

    【解决方案3】:

    您可以使用下面的代码来检查定位服务是否启用

     LocationManager lm = null;
     boolean gps_enabled,network_enabled;
        if(lm==null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        try{
        gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        }catch(Exception ex){}
        try{
        network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        }catch(Exception ex){}
    
       if(!gps_enabled && !network_enabled){
            dialog = new AlertDialog.Builder(context);
            dialog.setMessage(context.getResources().getString(R.string.gps_network_not_enabled));
            dialog.setPositiveButton(context.getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    // TODO Auto-generated method stub
                    Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
                    context.startActivity(myIntent);
                    //get gps
                }
            });
            dialog.setNegativeButton(context.getString(R.string.Cancel), new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    // TODO Auto-generated method stub
    
                }
            });
            dialog.show();
    
        }
    

    【讨论】:

    • 嗨,我的问题不是关于如何检查值,而是关于如何监听更改,这意味着如何在代码中获取用户已在设置中更改此位置访问权限的通知,然后获取更改后的值。
    • @GrIsHu 这是不正确的。你试过下面usman的解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    相关资源
    最近更新 更多