我正在尝试将一个 int 值从我的 Service 传递给 CallReceiver 类,不幸的是 CallReceiver.value 始终等于 0,即使在设置了另一个值之后也是如此。当我试图将它作为参数传递给构造函数时,情况完全相同,因此使用从服务调用的 setter 方法。真的没有办法传递任何数据吗?
服务:
SharedPreferences settings = getSharedPreferences("SETTINGS", 0);
int value = settings.getInt("value1", 0); // here the correct value is present, not 0.
CallReceiver mCallReceiver = new CallReceiver();
CallReceiver.value = value;
接收者:
public class CallReceiver extends BroadcastReceiver {
public int value;
public CallReceiver(int value) {
this.value = value;
}
public CallReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.v("value", String.valueOf(value)); // here "value" = 0.
}
}