PhoneStateListener不会调用

时间:2010-12-07 15:20:28

标签: 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> 

1 个答案:

答案 0 :(得分:5)

使用后会创建监听器。