android - PhoneStateListener 不调用

标签 android android-emulator phone-state-listener

这是我的完整代码......

BroadcastExample.java

 package com.example.broadcast;

> import android.app.Activity; import
> android.content.Context; import
> android.os.Bundle; import
> android.telephony.*; import
> android.util.Log; import
> android.widget.TextView;

public class BroadcastExaple extends Activity {
 TextView textOut;
 TelephonyManager telephonyManager;
 PhoneStateListener listener;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {

  try {

   super.onCreate(savedInstanceState);

   // Get the UI
   textOut = new TextView(this);
   textOut.setText("DEmoBroadCast");
   setContentView(textOut);

   // Get the telephony manager
   telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

   // Register the listener wit the telephony manager
   telephonyManager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);

   // Create a new PhoneStateListener
   listener = new PhoneStateListener() {

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
     Log.d("DEBUG", "Phone listener....");
     String stateString = "N/A";
     switch (state) {
     case TelephonyManager.CALL_STATE_IDLE:
      stateString = "Idle";
      break;
     case TelephonyManager.CALL_STATE_OFFHOOK:
      stateString = "Off Hook";
      break;
     case TelephonyManager.CALL_STATE_RINGING:
      stateString = "Ringing";
      break;
     }
     textOut.append(String.format("\nonCallStateChanged: %s",
       stateString));
    }
   };

  } catch (Exception e) {

  }

 }
}

AndroidManifest.xml

-<?xml version="1.0" encoding="utf-8"?>
-<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.broadcast" android:versionCode="1"
 android:versionName="1.0">
 -<application android:icon="@drawable/icon" android:label="@string/app_name">
  -<activity android:name=".BroadcastExaple" android:label="@string/app_name">
   -<intent-filter>
    -<action android:name="android.intent.action.MAIN" />
    -<category android:name="android.intent.category.LAUNCHER" />
   -</intent-filter>
  -</activity>
 --</application>
 -<uses-prmission android:name="android.permission.READ_PHONE_STATE" />
 -<uses-sdk android:minSdkVersion="7" />
-</manifest> 

最佳答案

监听器在使用后创建。

关于android - PhoneStateListener 不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4378260/

相关文章:

android - 在 HW 模式下创建 OMXCodec 编码器

android - 如何连接我的 android HTC 设备进行调试?

android - 从 PHONE_STATE Intent 操作中读取数字 android 9 不工作

android - 如何正确地动态检查android权限

android - 更换手机后如何恢复RSA key ?

java - Android 解析主机名到 ip 地址转换

performance - 在 Windows 上选择 Google API(API 级别 17 或任何其他 API 级别)时,Android 模拟器不会加载

安卓浏览器插件

android - 如何以编程方式接收来电?