检索我的Andr​​oid应用程序呼叫号码 [英] retrieve the call number in my android APP

查看:110
本文介绍了检索我的Andr​​oid应用程序呼叫号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,当我收到我的app.I打电话想找回它,它存储在某处拿到号码呼叫。

I would like to get the number call when i receive a call in my app.I would like to retrieve it and store it somewhere.

public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);

.........

.........

incomingNumber包含什么。

incomingNumber contains nothing.

推荐答案

下面包含在你的应用程序的权限表现为应用程序的子

Include following permissions in your application manifest as a child of application.

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

还包括清单接收器

Also include a receiver in manifest

<!-- SAMPLE APPLICATION IS NAME OF OUR APPLICATION -->
<receiver android:name="SampleApplication" 
    android:enabled="true"
    android:exported="true"
    android:permission="">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>

</receiver>

创建一个类,扩展了 android.telephony.PhoneStateListener 并执行其 onCallStateChanged

public class 
class MyPhoneStateListener extends PhoneStateListener
  {
      public void onCallStateChanged(int state, String incomingNumber)
      {
          Log.d("SampleApp","Incoming call from: "+incomingNumber);
      }
  }

现在你的OnCreate()调用TelephonyManager并添加这个类作为监听器:

Now in you onCreate() call TelephonyManager and add this class as listener:

TelephonyManager myTelManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
MyPhoneStateListener myListner = new MyPhoneStateListener();
myTelManager.listen(myListner, PhoneStateListener.LISTEN_CALL_STATE);

您可以在LogCat中签结果。

You can check result in LogCat.

编辑:完整的类存储在不同反应:

import android.app.Activity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class SampleApp extends Activity{

private String incomingCallNumber="";
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.rating);
    TelephonyManager myTelManager = 
(TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
    MyPhoneStateListener myListner = new MyPhoneStateListener();
    myTelManager.listen(myListner, PhoneStateListener.LISTEN_CALL_STATE);


}
class MyPhoneStateListener extends PhoneStateListener
{
  public void onCallStateChanged(int state, String incomingNumber)
  {
     SampleApp.this.incomingCallNumber=incomingNumber;
  }
}

}

这篇关于检索我的Andr​​oid应用程序呼叫号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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