0

Android/Java 新手,我正在玩 AddProximityAlert()。我在下面有以下代码块,并且不断收到错误。有人可以解释 Eclipse 在说什么,以及如何解决我为 getBroadcast 和 registerReceiver 遇到的错误吗?

在 SecondaryActivity.java 文件中,我有 2 个广播接收器,这可能是原因吗?

SecondaryActivity.java:

public class SecondaryActivity extends BroadcastReceiver {   

public void onReceive(Context context, Intent intent) {

.
.
.

lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

double latitude = location.getLatitude();
double longitude = location.getLongitude();
float radius = 100f;
long expiration = -1;
final String PROX_ALERT_INTENT = "com.example.test";

Intent intent = new Intent(PROX_ALERT_INTENT);
PendingIntent proximityIntent = PendingIntent.**getBroadcast**(this, 0, intent, 0);

lm.addProximityAlert(latitude, longitude, radius, expiration, proximityIntent);

IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);

**registerReceiver**(new ProximityIntentReceiver(), filter);

.
.
.

private class ProximityIntentReceiver extends BroadcastReceiver {

...

getBroadcast : PendingIntent 类型中的方法 getBroadcast(Context, int, Intent, int) 不适用于参数 (SecondaryActivity, int, Intent, int)

registerReceiver:方法 registerReceiver(SecondaryActivity.ProximityIntentReceiver, IntentFilter) 未定义为 SecondaryActivity 类型

4

1 回答 1

1

因为您正在扩展广播接收器,所以您需要使用:

PendingIntent.getBroadcast(context, 0, intent, 0)

context.registerReceiver()

引用 Context 类。

命名广播接收SecondaryActivity器也有点令人困惑,因为SecondaryActivity它不是一个活动。

于 2012-10-22T18:48:06.017 回答