私はアンドロイドが初めてです。mysql データベースから取得したさまざまな場所に近接アラートを追加しようとしています。場所を取得して、それらを arrayList に保存します。
ArrayList<String> latArray, lngArray;
現在、次のような場所に近接アラートを追加しています。
for (int i = 0; i < latArray.size(); i++) {
setProximityAlert(Double.valueOf(latArray.get(i)),Double.valueOf(lngArray.get(i)));
}
private void setProximityAlert(double latitude, double longitude) {
Intent intent = new Intent(IntentToFire);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
locationManager.addProximityAlert(latitude, longitude, radius,expiration, proximityIntent);
// register the broadcast receiver to start listening for proximity alerts
filter = new IntentFilter(IntentToFire);
registerReceiver(myReceiver, filter);
}
今、私のブロードキャストレシーバークラスは次のとおりです。
public class MyProximityAlertReceiver extends BroadcastReceiver{
private static final int NOTIFICATION_ID = 1000;
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING; //indicates whether proximity alert is entering or exiting
//Retrieve extended data from the intent and return false if no value of the desired type is stored with the
given name.
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
Log.d(getClass().getSimpleName(), "entering");
System.out.print("entering");
}
else {
Log.d(getClass().getSimpleName(), "exiting");
System.out.print("exiting");
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//Retrieve a PendingIntent that will start a new activity
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);
//create icons and other stuffs for notification
Notification notification = createNotification();
notification.setLatestEventInfo(context,
"Proximity Alert!", "In the area.", pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
........
}
通知を受け取るとすぐに、次のエラーが表示されます。
エラーコードの数行をダブルクリックすると、次の行に移動します。
notificationManager.notify(NOTIFICATION_ID, notification);
また、一時停止時と再起動時にレシーバーの登録を解除して再登録しています。
@Override
protected void onPause() {
unregisterReceiver(myReceiver);
super.onPause();
}
@Override
protected void onRestart() {
registerReceiver(myReceiver, filter);
super.onRestart();
}
私が間違っていることを理解していませんか?近接アラートを追加するときに何か間違ったことをしていますか? どんな助けでも感謝します。