<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>
那么我的服务类就是这样 –
public class ServiceReceiver extends broadcastReceiver {@OverrIDepublic 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是 –
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);}
}
我可以看到IDLE状态.
但是当我打电话给我没有铃声状态.为什么?
解决方法 我想你正在混合两种方法来获取电话状态.如果您使用意向过滤器和广播接收器,则在接收器中不需要调用TelephonyManager的Listen().只需检查收到的意图如下: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"); }}总结
以上是内存溢出为你收集整理的android – 无法获取TelephonyManager.CALL_STATE_RINGING全部内容,希望文章能够帮你解决android – 无法获取TelephonyManager.CALL_STATE_RINGING所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)