简体   繁体   English

在android中通话期间的来电号码?

[英]Incoming number during a call in android?

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 CustomBroadcastReceiver.java此类将接收操作电话状态更改,并将实例化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 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 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>

Thnx for the answers...I tried another approach and following code seems to work..Joined both receiver and phonestatelistener.. 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?? Thnx的答案...我尝试了另一种方法,下面的代码似乎可行..加入了接收器和phonestatelistener .. 但是现在的问题是我必须重新启动设备才能启动此服务 ..任何建议我在哪里现在错了吗?

public class IncomingCallReciever extends BroadcastReceiver {

private Context mContext;
private Intent mIntent;

@Override
public void onReceive(Context context, Intent intent) {
    mContext = context;
    mIntent = intent;
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    int events = PhoneStateListener.LISTEN_CALL_STATE;
    tm.listen(phoneStateListener, events);
}

private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        String callState = "UNKNOWN";
        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            callState = "IDLE";
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            // -- check international call or not.
            if (incomingNumber.startsWith("00")) {
                Toast.makeText(mContext,"International Call- " + incomingNumber,Toast.LENGTH_LONG).show();
                callState = "International - Ringing (" + incomingNumber+ ")";
            } else {
                Toast.makeText(mContext, "Local Call - " + incomingNumber, Toast.LENGTH_LONG).show();
                callState = "Local - Ringing (" + incomingNumber + ")";
            }
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            String dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            if (dialingNumber.startsWith("00")) {
                Toast.makeText(mContext,"International - " + dialingNumber,Toast.LENGTH_LONG).show();
                callState = "International - Dialing (" + dialingNumber+ ")";
            } else {
                Toast.makeText(mContext, "Local Call - " + dialingNumber,Toast.LENGTH_LONG).show();
                callState = "Local - Dialing (" + dialingNumber + ")";
            }
            break;
        }
        Log.i(">>>Broadcast", "onCallStateChanged " + callState);
        super.onCallStateChanged(state, incomingNumber);
    }
};

}

The problem is in your manifest file, you must have this: 问题出在您的清单文件中,您必须具有以下内容:

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

instead of : 代替 :

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

"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... 您必须重新启动手机的原因是,您在AndroidManifest.xml文件中仅允许执行一项操作...

<!-- 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. 您已将BroadcastReceiver设置为仅在电话开机时启动。
To fix this, you need to use the answer provided by DR VOLT. 要解决此问题,您需要使用DR VOLT提供的答案。

Just add the actions PHONE_STATE and NEW_OUTGOING_CALL to your receivers 只需将动作PHONE_STATE和NEW_OUTGOING_CALL添加到您的接收者
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. 每当这些动作发生时,这将启动您的广播接收器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM