1

我想静态注册我的接收器,而不是动态注册。 对于动态注册,效果很好,我正在使用这个:

..
static private IntentFilter GPSActionFilter;
..
GPSActionFilter = new IntentFilter("GGPS Service");
context.registerReceiver(GPSActionReceiver, GPSActionFilter);
..
static private BroadcastReceiver GPSActionReceiver = new BroadcastReceiver(){  
    public void onReceive(Context context, Intent intent) {  ..}

对于静态注册,调试器永远不会点击 onReceive 函数,我正在使用这个:

在 AndoridManifest 中:

<receiver android:name="LocationListener$GPSActionReceiver" >   
    <intent-filter>
        <action android:name="LocationListener.ACTION_GPS_SERVICE" />
    </intent-filter>
</receiver>

在代码中:

public class LocationListener extends BroadcastReceiver
{
    public static IntentFilter GPSActionFilter;
    public static final String ACTION_GPS_SERVICE = "com.example.LocationListener.GPSActionFilter";
    public static BroadcastReceiver GPSActionReceiver;
    public void onReceive(final Context context, final Intent intent)
    {..}
}
4

2 回答 2

1

首先,清单中不能有 a private BroadcastReceiver,因为 Android 需要能够创建它的实例。请将此公之于众。

其次,静态内部类名称的语法是LocationListener$GPSActionReceiver, not LocationListener.GPSActionReceiver

于 2013-05-22T22:16:22.337 回答
1

在清单中声明意图过滤器操作时,android:name 必须是字符串文字,并且不能从类中访问字符串。另外,我建议您将完全限定的包名称添加到意图操作中,即:

public static final String GPS_SERVICE = "com.example.LocationListener.ACTION_GPS_SERVICE"

然后改变

<action android:name="LocationListener.GPS_SERVICE" />

<action android:name="com.example.LocationListener.ACTION_GPS_SERVICE" />
于 2013-05-22T22:20:50.630 回答