在Android的通话期间来电号码? [英] Incoming number during a call in android?

查看:137
本文介绍了在Android的通话期间来电号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在下面code,它的编译但我没有得到任何结果或吐司显示的请帮助...

CustomBroadcastReceiver.java 这个类将收到的动作手机状态变化,将实例化customephonestatelistener

 公共类CustomBroadcastReceiver扩展的BroadcastReceiver {

TelephonyManager电话=
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

CustomPhoneStateListener customPhoneListener =新CustomPhoneStateListener(上下文);

telephony.listen(customPhoneListener,PhoneStateListener.LISTEN_CALL_STATE);


捆绑包= intent.getExtras();
串phoneNr = bundle.getString(incoming_number);
    Log.v(TAG,phoneNr:+ phoneNr);
    吐司面包= Toast.makeText(背景下,phoneNr:+ phoneNr,Toast.LENGTH_LONG);
    toast.show();
}
}
 

CustomPhonestateListener.java

这个类是主要经营类

 公共类CustomPhoneStateListener扩展PhoneStateListener {

私有静态最后字符串变量=CustomPhoneStateListener;

私人语境mContext;

公共CustomPhoneStateListener(上下文的背景下){


// TODO自动生成构造函数存根
mContext =背景;
}

公共无效onCallStateChanged(INT状态,串incomingNumber){

    Log.v(TAG,我们都在里面!!!!!!!!!!!);

     Log.v(TAG,incomingNumber);
    吐司面包= Toast.makeText(mContext,我们都在里面!!!!!!!!!!!,Toast.LENGTH_LONG);
    toast.show();

    开关(州){
            案例TelephonyManager.CALL_STATE_RINGING:
                    Log.d(TAG,振铃);
                    打破;
    }
 

AndroidManifest.xml中

 <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_SMS/>
  <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
  <使用-权限的Andr​​oid:名称=android.permission.WRITE_CONTACTS/>
  <使用-权限的Andr​​oid:名称=android.permission.READ_PHONE_STATE/>

<应用
    机器人:图标=@可绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
    <接收机器人:MyBroadcastReceiverNAME =>
            <意向滤光器>
                    <作用机器人:名称=android.intent.action.PHONE_STATE/>

            &所述; /意图滤光器>
    < /接收器>
< /用途>
 

解决方案

@ nitinsh99我不能对你所提供的解决方案发表意见,所以我要在这里提供一个答案......

但现在的问题是,我需要重新启动设备,以启动该service..any建议,在那里我是错的,现在?

这是你必须重新启动你的手机的原因是,在你的Andr​​oidManifest.xml文件已经允许只有一个动作......

 <! - 你的问题是在这里 - >
<作用机器人:名称=android.intent.action.BOOT_COMPLETED/>
 

您已经确立了自己的BroadcastReceiver只有当手机启动时启动。
为了解决这个问题,你需要使用DR VOLT提供了答案。

只需添加行为PHONE_STATE和NEW_OUTGOING_CALL到您的接收器
例如:

 <接收机器人:名称=。MyReceiver>
<意向滤光器>
<作用机器人:名称=android.intent.action.PHONE_STATE/>
<作用机器人:名称=android.intent.action.NEW_OUTGOING_CALL/>
<作用机器人:名称=android.intent.action.BOOT_COMPLETED/>
&所述; /意图滤光器>
< /接收器>
 

这将开始您的广播接收器,只要这些动作发生。

I am running following code, its compiling but I am not getting any result or toast displayed please help...

CustomBroadcastReceiver.java this class will receive the action phone state change and will instantiate the customephonestatelistener

public class CustomBroadcastReceiver extends BroadcastReceiver {

TelephonyManager telephony = 
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener(context);

telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);


Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
    Log.v(TAG, "phoneNr: "+phoneNr);
    Toast toast = Toast.makeText(context, "phoneNr: "+phoneNr, Toast.LENGTH_LONG);
    toast.show();
}
}

CustomPhonestateListener.java

This class is main operating class

public class CustomPhoneStateListener extends PhoneStateListener {

private static final String TAG = "CustomPhoneStateListener";

private Context mContext;

public CustomPhoneStateListener(Context context) {


// TODO Auto-generated constructor stub
mContext = context;
}

public void onCallStateChanged(int state, String incomingNumber){

    Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");

     Log.v(TAG, incomingNumber);
    Toast toast = Toast.makeText(mContext, "WE ARE INSIDE!!!!!!!!!!!", Toast.LENGTH_LONG);
    toast.show();

    switch(state){
            case TelephonyManager.CALL_STATE_RINGING:
                    Log.d(TAG, "RINGING");
                    break;
    }       

AndroidManifest.xml

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name=".MyBroadcastReceiver">
            <intent-filter>
                    <action android:name="android.intent.action.PHONE_STATE" />

            </intent-filter>
    </receiver>
</application>

解决方案

@nitinsh99 I cannot comment on the solution you provided, so I have to provide an answer here...

"But now the problem is I have to reboot the device in order to start this service..any suggestion where I am being wrong now??"

The reason that you have to restart your phone is that in your AndroidManifest.xml file you have allowed for only one action...

<!-- Your Problem is here -->
<action android:name="android.intent.action.BOOT_COMPLETED" />

You have set your BroadcastReceiver to only start when the phone is BOOTED.
To fix this, you need to use the answer provided by DR VOLT.

Just add the actions PHONE_STATE and NEW_OUTGOING_CALL to your receivers
For Example:

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

This will then start your broadcast receiver whenever these actions occur.

这篇关于在Android的通话期间来电号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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