如何知道我是否在Android上的电话? [英] How to know whether I am in a call on Android?
本文介绍了如何知道我是否在Android上的电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道我是否在通话中。
I want to know whether I am in a call.
如果我在一个电话然后启动服务(服务部分是明显的)。如何做到这一点?
If I am in a call then start the service (service part is clear). How do I do this?
在出席我需要拨打服务电话......我不知道如何做到这一点?任何帮助?
While attending the call I need to call the service... I am unaware of how to do this? Any help?
推荐答案
您需要的广播接收器...
You need broadcast receiver ...
在清单申报广播接收器...
In manifest declare broadcast receiver ...
<receiver android:name=".PhoneStateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
另外申报用途,权限...
Also declare uses-permission ...
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
广播接收机类...
The broadcast receiver class ...
package x.y;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class PhoneStateBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}
}
和一个类来定制手机状态听众......
And one class to customize phone state listener...
package x.y;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class CustomPhoneStateListener extends PhoneStateListener {
//private static final String TAG = "PhoneStateChanged";
Context context; //Context to make Toast if required
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//when Idle i.e no call
Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//when Off hook i.e in call
//Make intent and start your service here
Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
//when Ringing
Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
这篇关于如何知道我是否在Android上的电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文