Android锁屏提醒

1.锁屏显示的Activity如下配置

<activity
            android:name=".activity.ScreenLockActivity"
            android:excludeFromRecents="true"
            android:launchMode="singleInstance"
            android:noHistory="false"
            android:screenOrientation="portrait"
            android:taskAffinity="com.gy.fylzj.sanfang.activity.ScreenLockActivity"
            android:theme="@android:style/Theme.Wallpaper.NoTitleBar" />

2.onCreate中添加

     getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

当跳转至上面的activity时,即可在锁屏状态下唤醒屏幕,并显示activity

播放系统铃声

Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        rt = RingtoneManager.getRingtone(getApplicationContext(), uri);
        rt.play();

开启震动

 Vibrator  vib = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
        if (vib != null) {
            vib.vibrate(new long[]{1000, 200, 200, 200}, 0);
        }

在onDestroy中停止播放,取消震动

    @Override
    protected void onDestroy() {
        super.onDestroy();
        rt.stop();
        vib.cancel();
        ToastUtils.info(ScreenLockActivity.this,"已挂断");
 
    }

屏蔽BACK键

  @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            ActivityUtils.showYesOrNoDialog(this, "提示", "是否忽略视频指令", "确定", "取消",
                    new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    ScreenLockActivity.this.finish();
                }
            }, true);
        }
        return super.onKeyDown(keyCode, event);
    }

无法通过上面的方法屏蔽HOME键,只能通过广播监听到HOME键按下时,进行处理

    class InnerRecevier extends BroadcastReceiver {

        final String SYSTEM_DIALOG_REASON_KEY = "reason";


        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                if (reason != null) {
                    if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                        ScreenLockActivity.this.finish();
                    }
                }
            }
        }
    }

在onCreate中注册广播

    //创建广播
        innerReceiver = new InnerRecevier();
        //动态注册广播
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        //启动广播
        registerReceiver(innerReceiver, intentFilter);

在onDestroy中取消注册

unregisterReceiver(innerReceiver);

 

展开阅读全文

没有更多推荐了,返回首页