나는 해결책을 찾기 위해 많은 시간을 봤지만 그것은 효과가 없었다. 며칠 동안이 문제로 어려움을 겪고 계시다면이 문제에 직면하여 해결해주십시오.wifi 또는 GPS를 켜고 끌 때 내 BroadcastReceiver가 두 번 호출 되었습니까?
수신자가 Wi-Fi 또는 GPS를 켜거나 끌 때 두 번 트리거하지만 두 번 트리거하면 앱에 대한 내 계획이 손상 될 수 있습니다. 사전에
들으 ... 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>
이것을 브로드 캐스트 리시버 클래스 :
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) {
}
// ...
항상 수신기가 활성화되어 있어야합니까, 아니면 앱이 실행되는 동안에 만 필요합니까? –
배경에서도 활발히 활동해야합니다. 왜 그에게 @SerjArdovic을 물어 보셨습니까? – tahaDev
두 번 트리거하면 두 가지 의도의 작업은 무엇이 수신됩니까? 그리고'onReceive' 두 번 호출 사이에 얼마나 많은 시간이 있습니까? – clownba0t