我已经用谷歌搜索了很多次来寻找解决方案,但是没有起作用。与这个问题斗争了很多天,如果你遇到这个问题并解决了它,请帮助我。
当我打开或关闭wifi或Gps时,接收器会触发两次,虽然有时只触发一次,但如果触发两次,就会破坏我对这款应用的计划。
提前进行thx。
manifest.xml内部:
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<action android:name="android.location.PROVIDERS_CHANGED"/>
</intent-filter>
</receiver>
这是BroadcastReceiver类:
public class MyBroadcastReceiver extends BroadcastReceiver {
String TAG_NETW = "NetworkConnctivityUtils";
@Override
public void onReceive(Context context, Intent intentR) {
try {
// MyApplication.dbApp = new SqliteHelper(context, ConstHelper.DATABASE_PATH, ConstHelper.DATABASE_NAME);
SimpleDateFormat sdf = new SimpleDateFormat(ConstHelper.Simple_Date_Format);
if (intentR.getAction().matches("android.location.PROVIDERS_CHANGED")) {
LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d(TAG_NETW, "locationGps" + " ------ event On " + intentR.getAction());
StatusDriverQueryHelper.insertStatusDriverlog(EnumDeviceStatus.GPSOn, "0", AuthenticateHelper.getDeviceId(context, ""), UnicodeHelper.numberToEnglish(sdf.format(new Date())));
StatusDriverQueryHelper.showItems();
} else if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// todo : if at this time of other provider change the gps was of / or no what will be the status
Log.d(TAG_NETW, "locationGps" + " ------ event Off " + intentR.getAction());
StatusDriverQueryHelper.insertStatusDriverlog(EnumDeviceStatus.GPSOff, "0", AuthenticateHelper.getDeviceId(context, ""), UnicodeHelper.numberToEnglish(sdf.format(new Date())));
StatusDriverQueryHelper.showItems();
}
LogService.sendAllStatus();
} else {
}
} catch (Exception e) {
Log.d(TAG_NETW, "locationGps" + " error On GPS EVENT Reciver " + intentR.getAction());
}
/***************************/
try {
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intentR.getAction())) {
int status = NetworkConnctivityUtils.getConnectivityStatusString(context);
Log.e(TAG_NETW, " here : " + intentR.getAction() + " , STATUS : " + String.valueOf(status));
// Wifi is connected
if (status == NetworkConnctivityUtils.NETWORK_STATUS_NOT_CONNECTED) {
Log.d(TAG_NETW, "CONNECTIVITY" + " Not-----Available");
} else if (status == NetworkConnctivityUtils.NETWORK_STAUS_WIFI) {
Log.d(TAG_NETW, "CONNECTIVITY" + " OK wifi");
} else if (status == NetworkConnctivityUtils.NETWORK_STATUS_MOBILE) {
Log.d(TAG_NETW, "CONNECTIVITY" + " OK mobile");
} else {
Log.d(TAG_NETW, "CONNECTIVITY" + " nonononon ---");
}
}
} catch (Exception e) {
}
}
}
*找到了解决方法
但我不知道这是不是最好的处理方式:
我使用一个标志来检查它是否是第一次,然后在1到2秒后返回标志。
// ....
@Override
public void onReceive(Context context, Intent intentR) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(ConstHelper.Simple_Date_Format);
if (intentR.getAction().matches("android.location.PROVIDERS_CHANGED")) {
LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
if (ConstHelper.GpsActionisDoneOnce == false) {
ConstHelper.GpsActionisDoneOnce = true;
new Handler().postDelayed(new Runnable() {
public void run() {
ConstHelper.GpsActionisDoneOnce = false;
}
}, 500);
// GPS ACTION/CHECKING ..
} else {
}
}
} catch (Exception e) {
}
// ......
发布于 2017-12-19 00:41:37
不推荐使用广播接收器,因为这会降低系统性能。正确的方法是使用JobScheduler。
https://stackoverflow.com/questions/47872186
复制相似问题