java – 怎么样?侦听位置设置被打开(Android App)
所以我花了几个星期的时间在我的
Android应用程序上工作,并研究实现我需要做的最好的方式,但仍然无法正确完成..任何/所有帮助都非常感谢,因为我仍然掌握着一切……
任务: (假设“位置”/ GPS设置当前已关闭),我需要让我的应用程序一直在监听“位置”设置是否打开..此时,只需启动一个活动. 思路: 这些是我认为可能有用的不同方式: >使用“onProviderEnabled”的LocationListener 问题: 任何以下任何问题的建议或答案都非常感谢.. >上述哪些想法是完成任务的最佳方式?越简单越好,但最重要的是它需要一直监听,并在打开位置设置时立即响应. 我非常感谢你能给我的任何建议或帮助..我仍然掌握所有这一切,但有足够的信心去做,并渴望发布我的第一个应用程序..所以谢谢你,这意味着很多并将极大地帮助我. 编辑: 好的,这就是我到目前为止所得到的…… 继承人我的接管人: 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,DialogActivity.class); context.startActivity(runDialogActivity); } } // onReceive CLOSE BRACKET } // CLOSE OF FULL CLASS } 而且这是Manifest的样子: 的manifest.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> 除了这些文件,我还有以下文件: 因此,如果我理解正确,当用户下载应用程序并打开它时,我的MainActivity.Java和相应的布局将启动.但我只是将其用作首选屏幕.因此,一旦他们第一次打开应用程序,广播接收器应该自动开始侦听LOCATION设置是否打开,是否正确?我还希望广播监听器保持监听,即使它收到初始广播后(如果他们关闭LOCATION设置我的onReceive仍会触发,然后再将它们再次打开..) 那么(A)到目前为止我的代码看起来如何? 再次感谢您的帮助! 解决方法
由于您不需要实际获取位置,因此您需要的最佳实现是BroadcastReceiver.
这是最好的选择,因为您不需要一直运行服务(导致额外的电池消耗),并且您将能够从BroadcastReceiver启动您的活动. 使用intent过滤器和BroadcastReceiver,只要位置设置已更改(启用或禁用),操作系统就会启动您的应用程序,如果已启用,则可以从BroadcastReceiver启动活动. 首先添加intent过滤器,当操作系统发出设置已更改的隐式Intent时将捕获该过滤器: <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> 然后,在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); } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |