在片段中使用TelephonyManager?

时间:2013-06-26 09:22:12

标签: android service phone-call

我想在设备运行时调用启动和停止服务,所以我使用TelephonyManager并且我的类扩展了标记。我试过这段代码

 TelephonyManager tm = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);


    private PhoneStateListener mPhoneListener = new PhoneStateListener() {
    public void onCallStateChanged(int state, String incomingNumber) {

            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                getActivity().stopService(new Intent(getActivity(),LockScreenService.class));

                break;

            case TelephonyManager.CALL_STATE_OFFHOOK:
                getActivity().stopService(new Intent(getActivity(),LockScreenService.class));
                break;

            case TelephonyManager.CALL_STATE_IDLE:
                getActivity().startService(new Intent(getActivity(),LockScreenService.class));

                break;

            }
        } 
    } 
;

但它不起作用。请给我一些建议。非常感谢

1 个答案:

答案 0 :(得分:1)

试试这段代码:

 telephonyManager  =  ( TelephonyManager )getActivity.getSystemService( Context.TELEPHONY_SERVICE );
 imeistring = telephonyManager.getDeviceId();