我有一个BroadcastReceiver的问题。如果我在清单文件中这样声明action:
<receiver android:name="com.app.activity.observer.DataEntryObserver" >
<intent-filter>
<action android:name= "@string/action_db_updated" />
</intent-filter>
</receiver>
在 strings.xml 文件中我有:
<string name="action_db_updated">com.app.DB_UPDATED</string>
一切运作良好。但如果我进行如下更改:
<receiver android:name="com.app.activity.observer.DataEntryObserver" >
<intent-filter>
<action android:name= "com.app.DB_UPDATED" />
</intent-filter>
</receiver>
当调用接收器时,我遇到了这个异常:
java.lang.RuntimeException: 无法实例化接收器 com.app.activity.observer.DataEntryObserver:java.lang.InstantiationException: 无法实例化类 com.app.activity.observer.DataEntryObserver;没有空构造函数
我想保留工作版本,但Play商店不允许我发布应用,因为它需要一个字符串值,而不是一个变量 @string/..
我的接收器是一个外部类,并定义为:
public class DataEntryObserver extends BroadcastReceiver{
private AppUsageLoader dLoader;
public DataEntryObserver(AppUsageLoader dLoader) {
this.dLoader = dLoader;
IntentFilter filter = new IntentFilter(
ReaLifeApplication.ACTION_DB_UPDATED);
dLoader.getContext().registerReceiver(this, filter);
}
@Override
public void onReceive(Context arg0, Intent arg1) {
// Tell the loader about the change.
dLoader.onContentChanged();
}
}
startActivity
。 - IgorGanapolsky