Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在片段中使用TelephonyManager?_Android_Service_Phone Call - Fatal编程技术网

Android 在片段中使用TelephonyManager?

Android 在片段中使用TelephonyManager?,android,service,phone-call,Android,Service,Phone Call,我想在设备调用时启动和停止服务,所以我使用TelephonyManager和我的类扩展标记。我试过这个密码 TelephonyManager tm = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE); tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); private PhoneStat

我想在设备调用时启动和停止服务,所以我使用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;

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

请尝试以下代码:

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

您面临的问题是什么?我的服务被强制关闭。请附加日志以了解根本原因