没能拿到TelephonyManager.CALL_STATE_RINGING [英] Not able to get the TelephonyManager.CALL_STATE_RINGING
本文介绍了没能拿到TelephonyManager.CALL_STATE_RINGING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我说这是我的manifest文件 -
I added this is my manifest file -
<receiver android:name=".ServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
然后,我的服务类是这样的 -
Then my service class is like this -
public class ServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MyPhoneStateListener phoneListener = new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
和我PhoneStateListener是 -
and my PhoneStateListener is -
public class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
Log.i("telephony-example", "State changed: " + stateName(state));
}
String stateName(int state) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "***********IDLE********");
return "Idle";
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "***********OFFHOOK********");
return "Off hook";
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "***********RINGING********");
return "Ringing";
}
return Integer.toString(state);
}
}
我能看到空闲状态。
不过,当我打电话我不明白振铃状态。为什么呢?
But When I call i dont get the Ringing state. Why?
推荐答案
我想你混淆了两种方法来获取手机状态。如果您使用的意图过滤器和广播接收器,然后在接收端不需要调用TelephonyManager的听()。只是检查所接收到的意图是这样的:
I think you are mixing up two approaches to get phone state. If you use the intent-filter and broadcast receiver, then in the receiver no need to call the TelephonyManager's listen(). Just check the received intent like this :
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state))
{
Log.d("MPR", "Its Ringing [" + number + "]");
}
if (TelephonyManager.EXTRA_STATE_IDLE.equals(state))
{
Log.d("MPR", "Its Idle");
}
if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state))
{
Log.d("MPR", "Its OffHook");
}
}
这篇关于没能拿到TelephonyManager.CALL_STATE_RINGING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文