【问题标题】:PhoneStateListener doesn't callPhoneStateListener 不调用
【发布时间】:2012-07-26 18:12:58
【问题描述】:

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

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> 

【问题讨论】:

  • 以上程序不起作用.....请查找是否有任何错误...
  • @user533863:如果您不打算花时间解释“不起作用”,那么很少有人会花时间随机猜测什么“不起作用”。
  • 好的.....上面的程序没有进入phonestatelistener.......这就是我发现的......

标签: android android-emulator phone-state-listener


【解决方案1】:

监听器是在使用后创建的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 2013-02-02
    相关资源
    最近更新 更多