获取以下错误消息
The method findViewById is undefined for the type new PhoneStateListener()
所以我的问题是将活动的引用传递给BroadCastReceiver子类的正确方法是什么?
我的BroadcastReceiver子类位于
之下package com.example.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.EditText;
public class ServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
Log.d("foo","incomingNumber : "+incomingNumber);
EditText incomingNum = (EditText) findViewById(R.id.inputText);
incomingNum.setText(incomingNumber);
}
},PhoneStateListener.LISTEN_CALL_STATE);
}
}
答案 0 :(得分:1)
您可以在Class
内创建匿名内部BroadcastReceiver
,而不是为Class
创建单独的Activity
。然后,您可以通过执行Activity
findViewById()
的{{1}}