添加PhoneStateListenerPhoneStateListener

由网友(づ幻滅的夢↗)分享简介:我特林设立PhoneStateListener但我得到一个PhoneCallListener不能被解析为一个类型。I tring to set up PhoneStateListener but I get a "PhoneCallListener cannot be resolved to a type".pub...

我特林设立PhoneStateListener但我得到一个PhoneCallListener不能被解析为一个类型。

I tring to set up PhoneStateListener but I get a "PhoneCallListener cannot be resolved to a type".

public class ButtonView extends FrameLayout  {
.
.
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
.
.
}

在我发现那位其writen像其他程序和它的工作

in another program that I fount its writen like that and it's working

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {

        // add PhoneStateListener
        PhoneCallListener phoneListener = new PhoneCallListener();
        TelephonyManager telephonyManager = (TelephonyManager) this
            .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
}

什么建议立即进行删除我在我的改变,使其工作? 感谢您的帮助

what shoul i change in mine to make it work?? thanks for help

推荐答案

你必须创建reciever赶电话,

Hi you have to create reciever to catch phone calls,

要做到这一点,

在manifest.xml的

in manifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<receiver android:name=".ServiceReceiver">
    <intent-filter>
      <action android:name="android.intent.action.PHONE_STATE" />
   </intent-filter>
</receiver>

下面创建

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.webkit.WebView;

public class MyPhoneStateListener extends PhoneStateListener {

    public static Boolean phoneRinging = false;

    public void onCallStateChanged(int state, String incomingNumber) {

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            Log.d("DEBUG", "IDLE");
            phoneRinging = false;
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            Log.d("DEBUG", "OFFHOOK");
            phoneRinging = false;
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            Log.d("DEBUG", "RINGING");
            phoneRinging = true;

            break;
        }
    }

}


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class ServiceReceiver extends BroadcastReceiver {
    TelephonyManager telephony;

    public void onReceive(Context context, Intent intent) {
        MyPhoneStateListener phoneListener = new MyPhoneStateListener();
        telephony = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    public void onDestroy() {
        telephony.listen(null, PhoneStateListener.LISTEN_NONE);
    }

}
阅读全文

相关推荐

最新文章