Android,TelephonyManager,PhoneStateListener和传入号码的乐趣 [英] Android, TelephonyManager, the joys of PhoneStateListener and incoming numbers

查看:106
本文介绍了Android,TelephonyManager,PhoneStateListener和传入号码的乐趣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚进入Android开发领域,因此决定在这个新领域进行的第一项努力就是掌握电话对来电的反应.

I've very newly gotten into Android development, and decided that my first conquest on this fresh field would be to grasp how the phone reacted to incoming calls.

稍后进行一次谷歌搜索使我进入 http://www .compiletimeerror.com/2013/08/android-call-state-listener-example.html#.Vi3Ren4vfwM (所以我的代码与他/她有着惊人的相似之处.)

A little googling later led me to http://www.compiletimeerror.com/2013/08/android-call-state-listener-example.html#.Vi3Ren4vfwM (so my code shares a striking resemblance to his/hers).

我的主要活动(也是唯一的活动)如下:

My main (and only) activity looks like this:

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    TelephonyMgr.listen(new TeleListener(),
            PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
class TeleListener extends PhoneStateListener {
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                // CALL_STATE_IDLE;
                Log.d("MyLittleDebugger", "I'm in " + state + " and the number is " + incomingNumber);
                Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
                        Toast.LENGTH_LONG).show();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                // CALL_STATE_OFFHOOK;
                Log.d("MyLittleDebugger", "I'm in " + state + " and the number is " + incomingNumber);
                Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
                        Toast.LENGTH_LONG).show();
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                // CALL_STATE_RINGING
                Log.d("MyLittleDebugger", "I'm in " + state + " and the number is " + incomingNumber);
                Toast.makeText(getApplicationContext(), incomingNumber,
                        Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
                        Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }

}
}

现在,这就是乐趣停止的地方.我在模拟器上运行了该应用程序,并使用DDMS欺骗了对模拟设备的几个电话,以查看这些碎片在何处着陆.

Now, here's where the fun stops. I got the app running on emulator, and used DDMS to spoof a few phone calls to my emulated device to see where the pieces landed.

肯定有足够多的吐司突然冒出来,而MyLittleDebugger在状态交换时突然爆发了.侦听器正在工作,但是我的日志或吐司中没有显示任何数字.

And surely enough toast popped up and MyLittleDebugger flared up upon state swaps. The listener was working, however no number was ever being shown in my log or my toast.

数字应该在什么地方只是空白!不是null或其他任何东西,不是,而是空白!

It was just blank where the number should have been! Not null or anything, no, but blank!

经过更多的谷歌搜索后,我意识到我的AndroidManifest.xml可能是问题所在.如下:

After a little more googling, I realized that my AndroidManifest.xml might be the problem. It is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.x.xy" >

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

现在,这里的问题是:我想念什么?

Now, here's the question: what am I missing?

很显然,某处事情的一小部分出现了问题,因为我可以让我的TelephonyMgr对象.listen()调用状态,但是我无法显示该数字.

Clearly, a little fraction of a something has gone wrong somewhere, because I am able to have my TelephonyMgr object .listen() to call states, but I can't get the number to show.

新信息:

我也曾在手机上尝试过此操作,而没有模拟出完全相同的结果.

I've also tried this on my phone, without emulating to the exact same result.

推荐答案

您可能需要使用广播接收器,这可能对您尝试实现的目标有所帮助. 创建扩展广播接收器的类,并尝试捕获传入的号码.

You probably need to make use of broadcast receiver which may help you what you trying to achieve. create class extending broadcast receiver and in that try to catch the incoming number.

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        TelephonyManager mtelephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        mtelephony.listen(new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
               switch (state) {
                 case TelephonyManager.CALL_STATE_RINGING:
                // CALL_STATE_RINGING
                Log.d("MyLittleDebugger", "I'm in " + state + " and the number is " + incomingNumber);
            Toast.makeText(getApplicationContext(), incomingNumber,
                    Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
                    Toast.LENGTH_LONG).show();
            break;
        default:
            break;
               }   
            }
        },PhoneStateListener.LISTEN_CALL_STATE);
    }

,并在您的清单中也显示此行.

and in your manifest this line as well.

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

这篇关于Android,TelephonyManager,PhoneStateListener和传入号码的乐趣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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