如何?侦听“位置设置”是否打开(安卓应用)

因此,在过去的几周里,我一直在研究我的Android应用程序,并研究实现我需要做的事情的最佳方式,但仍然无法完全正确。任何/所有的帮助都非常感谢,因为我仍然掌握了事情的窍门。

任务:

(假设“位置”/GPS设置当前已关闭),我需要让我的应用程序不断侦听“位置”设置以打开。此时,只需启动一个活动。

想法:

这些是我认为可能起作用的所有不同方式:

  • LocationListener using “onProviderEnabled”

  • GpsStatusListener 使用“onGpsStatusChanged”和“GPS_EVENT_STARTED”

  • GpsProvider需要卫星(以确定它是否启动),或者以某种方式使用GpsProvider的“可用”常量/int

  • 设置注入器服务使用“ACTION_SERVICE_INTENT”(和/或)“ACTION_INJECTED_SETTING_CHANGED”和“onGetEnabled”或“isEnabled”

  • 设置。使用“LOCATION_MODE”的安全!= “LOCATION_MODE_OFF”

  • 内容观察者/内容解析器

  • Intent getAction (...)

  • 某种“如果/否则”

问题:

非常感谢以下任何问题的任何建议或答案。

  • 上述哪一个想法是完成任务的最佳方式?越简单越好,但最重要的是它需要始终倾听,并在打开位置设置时立即响应。

  • 对于上述想法中的哪一个最有效,我将如何实现它?(例如,我需要 BroadcastListener?还是 Service?它们是如何拼凑在一起的?

我真的很感激你能为我提供的任何建议或帮助。我仍然掌握了这一切的窍门,但有足够的信心去做,并渴望发布我的第一个应用程序。所以谢谢你,这意味着很多,会大大地帮助我。


编辑:

好的,这就是我到目前为止所得到的...
这是我的接收器:

MyReceiver.Java

public class MyReceiver extends BroadcastReceiver {

    private final static String TAG = "LocationProviderChanged";

    boolean isGpsEnabled;
    boolean isNetworkEnabled;



    public MyReceiver() {
    // EMPTY

    // MyReceiver Close Bracket
    }



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


        // PRIMARY RECEIVER
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {

            Log.i(TAG, "Location Providers Changed");

            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            Toast.makeText(context, "GPS Enabled: " + isGpsEnabled + " Network Location Enabled: " + isNetworkEnabled, Toast.LENGTH_LONG).show();

            // START DIALOG ACTIVITY
            if (isGpsEnabled || isNetworkEnabled) {
                Intent runDialogActivity = new Intent(context, DialogActivity.class);
                context.startActivity(runDialogActivity);

            }

        }



        // BOOT COMPLETED (REPLICA OF PRIMARY RECEIVER CODE FOR WHEN BOOT_COMPLETED)
        if (intent.getAction().matches("android.intent.action.BOOT_COMPLETED")) {

            Log.i(TAG, "Location Providers Changed Boot");

            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            Toast.makeText(context, "GPS Enabled Boot: " + isGpsEnabled + " Network Location Enabled Boot: " + isNetworkEnabled, Toast.LENGTH_LONG).show();

            // START DIALOG ACTIVITY
            if (isGpsEnabled || isNetworkEnabled) {
                Intent runDialogActivity = new Intent(context, DialogActivity.class);
                context.startActivity(runDialogActivity);

            }

        }



    // onReceive CLOSE BRACKET
    }



// CLOSE OF FULL CLASS
}

清单如下所示:

清单.XML

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ender.projects.receivertest">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".DialogActivity">
        </activity>

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.location.PROVIDERS_CHANGED" />

                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

除了这些文件之外,我还有以下文件:
和 ,两者都与布局文件一起使用,
并与它们匹配。

因此,如果我理解正确,当用户下载应用程序并打开它时,我将启动我和相应的布局。但我只会将其简单地用作首选项屏幕。因此,一旦他们第一次打开应用程序,广播接收器应该自动开始侦听要打开的位置设置,对吗?我还希望广播监听器保持收听状态,即使它收到初始广播(因此,如果他们关闭LOCATION设置,我的仍然会触发,然后他们再次将其打开。

那么(A)到目前为止,我的代码看起来如何?
(B)为了完成我刚才描述的事情,需要添加什么?
(C)当我打开位置设置时,运行它抛出此错误。
我该如何修复它?:



再次感谢所有帮助!MainActivity.JavaDialogActivity.Javaactivity_main.xmlactivity_dialog.xmlMainActivity.JavaonReceivejava.lang.RuntimeException: Unable to start receiver com.bryce.projects.servicesthreadsetc.MyReceiver: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?


答案 1

由于您不需要实际获取位置,因此满足您需求的最佳实现是广播接收器。

这是最好的选择,因为您不需要始终运行服务(导致额外的面糊消耗),并且您可以从广播接收者开始您的活动。

使用意向过滤器和广播接收器,每当位置设置更改(启用或禁用)时,操作系统就会启动您的应用程序,并且在启用的情况下,您可以从广播接收器启动您的活动。

首先添加意向筛选器,当操作系统发出设置已更改的隐式意向时,将捕获该筛选器。

<receiver
    android:name=".LocationProviderChangedReceiver"
    android:exported="false" >
    <intent-filter>
        <action android:name="android.location.PROVIDERS_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

请注意,要使其在 Android Oreo 及更高版本上运行,您需要在运行时注册广播接收器,请参阅此处:https://developer.android.com/guide/components/broadcasts#context-registered-receivers

然后,在LocationProviderChangedReceiver.java中,您的实现将如下所示:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;

public class LocationProviderChangedReceiver extends BroadcastReceiver {
    private final static String TAG = "LocationProviderChanged";

    boolean isGpsEnabled;
    boolean isNetworkEnabled;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED"))
        {
            Log.i(TAG, "Location Providers changed");

            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            //Start your Activity if location was enabled:
            if (isGpsEnabled || isNetworkEnabled) {
                  Intent i = new Intent(context, YourActivity.class);
                  context.startActivity(i);
            }
        }
    }
}

编辑

更新了 Kotlin 的解决方案,并在运行时为 Android 9 注册接收器。

Kotlin 中的 BroadcastReceiver 类:

class LocationProviderChangedReceiver : BroadcastReceiver() {

    internal var isGpsEnabled: Boolean = false
    internal var isNetworkEnabled: Boolean = false

    override fun onReceive(context: Context, intent: Intent) {
        intent.action?.let { act ->
            if (act.matches("android.location.PROVIDERS_CHANGED".toRegex())) {
                val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
                isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
                isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

                Log.i(TAG, "Location Providers changed, is GPS Enabled: " + isGpsEnabled)

                //Start your Activity if location was enabled:
                if (isGpsEnabled || isNetworkEnabled) {
                    val i = Intent(context, YourActivity::class.java)
                    context.startActivity(i)
                }
            }
        }
    }

    companion object {
        private val TAG = "LocationProviderChanged"
    }
}

在运行时注册,例如在应用的 MainActivity 的 onCreate() 中注册:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val br: BroadcastReceiver = LocationProviderChangedReceiver()
    val filter = IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION)
    registerReceiver(br, filter)
}

答案 2

下面是使用动态注册的解决方案:

在片段或活动的 onResume() 方法中,侦听LocationManager.PROVIDERS_CHANGED_ACTION

IntentFilter filter = new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION);
filter.addAction(Intent.ACTION_PROVIDER_CHANGED);
mActivity.registerReceiver(gpsSwitchStateReceiver, filter);

下面是该对象的代码示例:gpsSwitchStateReceiver

private BroadcastReceiver gpsSwitchStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {


            if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction())) {
                // Make an action or refresh an already managed state.

                LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

                if (isGpsEnabled || isNetworkEnabled) {
                    Log.i(this.getClass().getName(), "gpsSwitchStateReceiver.onReceive() location is enabled : isGpsEnabled = " + isGpsEnabled + " isNetworkEnabled = " + isNetworkEnabled);
                } else {
                    Log.w(this.getClass().getName(), "gpsSwitchStateReceiver.onReceive() location disabled ");
                }
            }
        }
    };

在 onPause() 方法中,注销接收器

mActivity.unregisterReceiver(gpsSwitchStateReceiver);

推荐