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:未为类型SecondaryActivity类型定义方法registerReceiver(SecondaryActivity.ProximityIntentReceiver,IntentFilter)

最佳答案

因为要扩展BroadcastReceiver,所以需要使用:

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




context.registerReceiver()


引用Context类。

另外,由于SecondaryActivity不是活动,因此为BroadcastReceiver SecondaryActivity命名也有些混乱。

关于java - 实现AddProximityAlert,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13017290/

10-09 10:06