PhoneStateListener不叫 [英] PhoneStateListener doesn't call

查看:307
本文介绍了PhoneStateListener不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的整个code .......

BroadcastExample.java

 包com.example.broadcast;>进口android.app.Activity;进口
> android.content.Context;进口
> android.os.Bundle;进口
> android.telephony *。进口
> android.util.Log;进口
> android.widget.TextView;公共类BroadcastExaple延伸活动{
 TextView中的TextOut;
 TelephonyManager telephonyManager;
 PhoneStateListener监听; / **当第一次创建活动调用。 * /
 @覆盖
 公共无效的onCreate(捆绑savedInstanceState){  尝试{   super.onCreate(savedInstanceState);   //获取UI
   的TextOut =新的TextView(本);
   textOut.setText(DEmoBroadCast);
   的setContentView(的TextOut);   //获取电话经理
   telephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);   //注册监听机智的电话经理
   telephonyManager.listen(监听器,PhoneStateListener.LISTEN_CALL_STATE);   //创建一个新PhoneStateListener
   听众=新PhoneStateListener(){    @覆盖
    公共无效onCallStateChanged(INT状态,弦乐incomingNumber){
     Log.d(调试,手机监听器......);
     字符串stateString =N / A;
     开关(州){
     案例TelephonyManager.CALL_STATE_IDLE:
      stateString =空闲;
      打破;
     案例TelephonyManager.CALL_STATE_OFFHOOK:
      stateString =摘机;
      打破;
     案例TelephonyManager.CALL_STATE_RINGING:
      stateString =振铃;
      打破;
     }
     textOut.append(的String.format(\\ nonCallStateChanged:%S,
       stateString));
    }
   };  }赶上(例外五){  } }
}

的Andr​​oidManifest.xml

   - <?XML版本=1.0编码=UTF-8>
- <清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 包=com.example.broadcast安卓版code =1
 机器人:=的versionName1.0>
  - <应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
   - <活动机器人:BroadcastExapleNAME =机器人:标签=@字符串/ APP_NAME>
    - &所述;意图滤光器>
     - <作用机器人:名字=android.intent.action.MAIN/>
     - <类机器人:名字=android.intent.category.LAUNCHER/>
    - &所述; /意图滤光器>
   - < /活性GT;
  - < /用途>
  - <采用-prmission机器人:名字=android.permission.READ_PHONE_STATE/>
  - <采用-SDK安卓的minSdkVersion =7/>
- < /清单>


解决方案

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

Here is My Whole Code.......

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> 

解决方案

Listener is created after using it.

这篇关于PhoneStateListener不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆