接收广播Intent时出错(有额外内容)

时间:2012-05-09 23:48:23

标签: android android-intent broadcastreceiver locationmanager android-pendingintent

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

                    }
........
}

收到通知后,我收到以下错误消息: 接收广播Intent {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();
    }

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

0 个答案:

没有答案