1

我是安卓新手。我正在尝试为从 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);

                    }
........
}

一旦我收到通知,我就会收到以下错误:接收广播意图时出错 { act=com.app (has extras) }

在双击几行错误代码时,它会将我带到这一行:

notificationManager.notify(NOTIFICATION_ID, notification);

我还在暂停和重新启动时取消注册和重新注册接收器:

@Override
    protected void onPause() {
        unregisterReceiver(myReceiver);
        super.onPause();
    }

    @Override
    protected void onRestart() {
        registerReceiver(myReceiver, filter);
        super.onRestart();
    }

我不明白我做错了什么?添加接近警报时我做错了吗?任何帮助表示赞赏。

4

0 回答 0