系统相关--仿QQ锁屏消息提醒

来源:互联网 发布:windows 桌面插件 编辑:程序博客网 时间:2024/06/06 10:50

一:仿QQ锁屏消息提醒

public class FollowupLockedScreen extends Activity implements View.OnClickListener, GestureDetector.OnGestureListener {    private ListView unReadMsgs;    private LinearLayout llLockedMsg;    private ImageView ivClose;    private GestureDetector gestureScanner;    private List<LockedMessageVO> patientNameList;    private NewMsgAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        final Window win = getWindow();        //FLAG_SHOW_WHEN_LOCKED:activity锁屏显示,FLAG_TURN_SCREEN_ON点亮屏幕        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);        setContentView(R.layout.activity_locked_msg);        initView();        initIntent(getIntent());        initGesture();    }    private void initIntent(Intent intent) {        LockedMessageVO lockedVO = new LockedMessageVO();        lockedVO.setTime(intent.getLongExtra("time", System.currentTimeMillis()));        lockedVO.setUserName(intent.getStringExtra("userName"));        if (patientNameList != null && patientNameList.size() > 0) {            boolean isExist = false;            for (LockedMessageVO vo : patientNameList) {                if (vo.getUserName() != null && lockedVO.getUserName() != null && vo.getUserName().equals(lockedVO.getUserName())) {                    isExist = true;                    vo.setTime(lockedVO.getTime());                    break;                }            }            if (!isExist) {                if(patientNameList.size()>=10){                    patientNameList.remove(patientNameList.size()-1);                }                patientNameList.add(0, lockedVO);            }        } else {            patientNameList.add(0, lockedVO);        }        adapter.notifyDataSetChanged();    }    /**     * 锁屏时或屏幕变暗时,收到消息打开activity后,若果屏幕变暗了,即使再来新消息,也不会点亮屏幕了,因此     * 要做下面的操作唤醒屏幕     */    private void wakeupScreen() {        PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);        if (!pm.isScreenOn()) {            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |                    PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");            wl.acquire();            wl.release();        }    }    @Override    protected void onNewIntent(Intent intent) {        super.onNewIntent(intent);        wakeupScreen();        initIntent(intent);    }    /**     * 实现双击功能     */    private void initGesture() {        gestureScanner = new GestureDetector(this);        gestureScanner.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {            public boolean onDoubleTap(MotionEvent e) {                //双击解锁进应用,连用户密码锁和手势都可以解,但是退出应用后右恢复锁屏状态,因此我注释掉了,双击后系统会提示用户自己解锁//                KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);//                if (km.inKeyguardRestrictedInputMode()) {//                    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);//                    KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("");//                    keyguardLock.disableKeyguard();//                }                Utils.startLoginActivity(FollowupLockedScreen.this, MainTabActivity.class);                finish();                return false;            }            public boolean onDoubleTapEvent(MotionEvent e) {                return false;            }            public boolean onSingleTapConfirmed(MotionEvent e) {                return false;            }        });    }    private void initView() {        patientNameList = new ArrayList<LockedMessageVO>();        adapter = new NewMsgAdapter(this, patientNameList);        llLockedMsg = (LinearLayout) findViewById(R.id.ll_locked_msg);//        initLayoutParams(llLockedMsg);        unReadMsgs = (ListView) findViewById(R.id.lv_locked_msg);        unReadMsgs.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                gestureScanner.onTouchEvent(event);                return false;            }        });        findViewById(R.id.iv_close).setOnClickListener(this);        unReadMsgs.setAdapter(adapter);    }    private void initLayoutParams(LinearLayout ll) {        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) ll.getLayoutParams();        layoutParams.width = MyApplication.width * 3 / 4;    }    @Override    public void onClick(View v) {        if (v.getId() == R.id.iv_close) {            finish();        }    }    @Override    public boolean onTouchEvent(MotionEvent event) {        gestureScanner.onTouchEvent(event);        return super.onTouchEvent(event);    }    @Override    public boolean onDown(MotionEvent e) {        return false;    }    @Override    public void onShowPress(MotionEvent e) {    }    @Override    public boolean onSingleTapUp(MotionEvent e) {        return false;    }    @Override    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {        return false;    }    @Override    public void onLongPress(MotionEvent e) {    }    @Override    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {        return false;    }}
    private void openLockedActivity(Context context, String userName) {        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);        KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);        boolean isScreenOn = powerManager.isScreenOn();        //屏幕变暗或锁屏时打开activity        if (!isScreenOn || km.inKeyguardRestrictedInputMode()) {            Intent alarmIntent = new Intent(context, FollowupLockedScreen.class);            alarmIntent.putExtra("userName", userName);            alarmIntent.putExtra("time", System.currentTimeMillis());            alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            context.startActivity(alarmIntent);        }    }



0 0
原创粉丝点击