End*_*r87 8 java gps android broadcastreceiver locationmanager
所以我花了几个星期的时间在我的Android应用程序上工作,并研究实现我需要做的最好的方法,但仍然无法正确完成..任何/所有帮助都非常感谢,因为我是仍然掌握着一切......
(假设"位置"/ GPS设置当前已关闭),我需要让我的应用程序一直在监听"位置"设置是否打开..此时,只需启动一个活动.
这些是我认为可能有用的不同方式:
LocationListener使用"onProviderEnabled"
使用"onGpsStatusChanged"和"GPS_EVENT_STARTED"的GpsStatusListener
GpsProvider需要卫星(以确定它是否启动),或以某种方式使用GpsProvider的"AVAILABLE"Constant/int
使用"ACTION_SERVICE_INTENT"(和/或)"ACTION_INJECTED_SETTING_CHANGED"与"onGetEnabled"或"isEnabled"的SettingInjectorService
Settings.Secure使用"LOCATION_MODE"!="LOCATION_MODE_OFF"
ContentObserver/ContentResolver
意图getAction(...)
某种"if/else"
任何以下任何问题的建议或答案都非常感谢..
以上哪些想法是完成任务的最佳方式?越简单越好,但最重要的是它需要一直监听,并在打开位置设置时立即响应.
对于上述哪个观点效果最好,我将如何实现它?(例如,我需要一个BroadcastListener?还是一个服务?它将如何组合在一起?
我非常感谢你能给我的任何建议或帮助..我仍然掌握所有这一切,但有足够的信心去做,并渴望发布我的第一个应用程序..所以谢谢你,这意味着很多并将极大地帮助我.
编辑:
好的所以这就是我到目前为止所得到的......
继承人
我的接收者:
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
}
Run Code Online (Sandbox Code Playgroud)
而且这是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>
Run Code Online (Sandbox Code Playgroud)
除了这些文件,我还有以下文件:
MainActivity.Java
和DialogActivity.Java
布局文件一起使用,
activity_main.xml
并activity_dialog.xml
与之匹配.
因此,如果我理解正确,当用户下载应用程序并打开它时,我的MainActivity.Java
相应布局将启动.但我只是将其用作首选屏幕.因此,一旦他们第一次打开应用程序,广播接收器应该自动开始侦听LOCATION设置是否打开,对吗?我还希望广播监听器保持监听,即使它收到初始广播后(onReceive
如果他们关闭LOCATION设置我仍然会触发,然后再将它们再次打开..
所以(A)我的代码看起来如何到目前为止?
和(B)要完成我刚刚描述的内容,需要添加什么?
和(C)当我打开LOCATION设置时运行它会抛出此错误.我
怎么能修复它?:
java.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?
再次感谢所有的帮助!
Dan*_*ent 23
由于您不需要实际获取位置,因此您需要的最佳实现是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>
Run Code Online (Sandbox Code Playgroud)
然后,在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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7469 次 |
最近记录: |