在锁屏界面长按紧急呼救按钮添加自动拨打112功能

该功能在紧急呼救按钮中添加onLongClick事件的处理即可。其中Intent.ACTION_CALL_EMERGENCY是紧急呼救的action。

修改文件/frameworks/base/packages/Keyguard/src/com/android/keyguard/EmergencyButton.java

public class EmergencyButton extends Button {

......

    @Override
    protected void onFinishInflate() {

......

//默认的点击跳转到拨号的代码

        setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                takeEmergencyCallAction();
            }
        });

//新加的长按拨号的代码
        setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v){
                Log.d(TAG, "test_ onLongClick");
                takeLongClickEmergencyCallAction();
                return true;
            }
        });
......

    /**
     * call 112.
     */
    public void takeLongClickEmergencyCallAction(){
        Log.d(TAG, "test_ takeLongClickCallAction");
        String phoneNumber = "112";
        Intent intentPhone = new Intent(Intent.ACTION_CALL_EMERGENCY, Uri.parse("tel:" + phoneNumber));
        intentPhone.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        getContext().startActivityAsUser(intentPhone,
                    new UserHandle(mLockPatternUtils.getCurrentUser()));
    }

    /**
     * Shows the emergency dialer or returns the user to the existing call.
     */
    public void takeEmergencyCallAction() {
        // TODO: implement a shorter timeout once new PowerManager API is ready.
        // should be the equivalent to the old userActivity(EMERGENCY_CALL_TIMEOUT)
        mPowerManager.userActivity(SystemClock.uptimeMillis(), true);
        if (mLockPatternUtils.isInCall()) {
            mLockPatternUtils.resumeCall();
        } else {
            final boolean bypassHandler = true;
            KeyguardUpdateMonitor.getInstance(mContext).reportEmergencyCallAction(bypassHandler);
            Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            getContext().startActivityAsUser(intent,
                    new UserHandle(mLockPatternUtils.getCurrentUser()));
        }
    }

......

这段代码中使用了Uri,需要import android.net.Uri;

 

posted @ 2016-10-26 19:23  夏叶星空  阅读(772)  评论(0编辑  收藏  举报