Android和GT;将活动的引用传递给BroadCastReceiver子类

时间:2013-12-17 23:09:09

标签: android broadcastreceiver

获取以下错误消息

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);
    }
}

1 个答案:

答案 0 :(得分:1)

您可以在Class内创建匿名内部BroadcastReceiver,而不是为Class创建单独的Activity。然后,您可以通过执行Activity

来引用findViewById()的{​​{1}}