mui集成安卓实现息屏退出,会话超时退出

前提:初次接触安卓系统,系统是MUI框架和H5和安卓,我想在安卓系统里面实现实现息屏的时候退出程序,超过30分钟退出程序。查阅资料后,可以用广播来接受锁屏息屏亮起的状态,当锁屏的时候退出程序。重写Activity的dispatchTouchEvent()的方法来监听触屏事件,写一个定时器,30分钟后退出程序,每次有监听事件初始化定时器。实现超过30分钟退出程序。但是,我写在MainActivity的广播,监听方法没有触发,这个就头疼。断点进不来,主方法集成了PandoraEntry,点开PandoraEntry

这是一个class文件,代表着他是写好的,我们调用他。这里面的oncreate,会被触发。于是我想到了,重写它,在这里添加广播和监听事件。

但是,我还是注释掉的它,因为,这里面也没有广播和监听方法。 

Android_ID_Utils android_ID_Utils=new Android_ID_Utils();这个是写的一个不能再模拟器下运行的一个判断方法。后续有时间在记录讲解,

没有仔细看代码。

他启动的是这个activity。于是 

 很简单。也是一个class。而且有一个构造方法,和一个不知道的方法,

 点开父类发现onCreate这个里面有,一直往上找,发现他没有继承activity这个类,,,所以它没有触屏这个方法,而且java是单继承,所以你不能在继承ctivity这个类,不能实现dispatchTouchEvent()这个触屏事件,我再找

发现这个方法是Callback里面的,

于是,我就重写了它,实现了Callback这个接口。重写dispatchTouchEvent()这个方法。断点终于进到这个方法里面了,广播也实现了,注意,这个activity要在配置

 这个是代码

package com.thinka.all;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window.Callback;

import org.zywx.wbpalmstar.widgetone.uex.gcjslr.TimeoutService;

import io.dcloud.WebAppActivity;
import io.dcloud.feature.internal.reflect.BroadcastReceiver;

public class PandoraEntryActivityLiunn extends WebAppActivity implements Callback{

    @Override
    public boolean onKeyDown(int var1, KeyEvent var2) {

        super.onKeyDown(var1, var2);
        return  false;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (this instanceof PandoraEntryActivityLiunn) { // 在APP的第一个activity处实例化
            if (ts == null) {
                Log.i(this.getClass().getName(), "创建timeoutService");
                ts = new TimeoutService(getApplicationContext());//必须传入全局context,以免activity被销毁后用这个context执行startActivity()报错
            }
            ts.start();
        }
        
        // 注册事件
        registerReceiver(myReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
        registerReceiver(myReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
        registerReceiver(myReceiver, new IntentFilter(Intent.ACTION_USER_PRESENT));
    }


    private static TimeoutService ts;
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i("dispatchTouchEvent", "按下");
                if (ts != null) {
                    ts.stop();
                }
                break;
            case MotionEvent.ACTION_UP:
                Log.i("dispatchTouchEvent", "抬起");
                if (ts != null) {
                    ts.start();
                }
                break;
        }
        return super.dispatchTouchEvent(event);
    }



    public static final String TAG = "TEST-----------------";
    public BroadcastReceiver myReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            if (Intent.ACTION_SCREEN_ON.equals(intent.getAction()) ) {//当按下电源键,屏幕亮起的时候

                Log.d(TAG,"<<sendBroadcast");
            }
            if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction()) ) {//当按下电源键,屏幕变黑的时候


                android.os.Process.killProcess(android.os.Process.myPid()); //获取PID
                System.exit(0);   //常规java、c#的标准退出法,返回值为0代表正常退出
                unregisterReceiver(myReceiver);

                Log.d(TAG,"<<sendBroadcast");
            }
            if (Intent.ACTION_USER_PRESENT.equals(intent.getAction()) ) {//当解除锁屏的时候

                Log.d(TAG,"<<sendBroadcast");

            }
        }
    };

}
package org.zywx.wbpalmstar.widgetone.uex.gcjslr;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import com.thinka.all.PandoraEntryActivityLiunn;

public class TimeoutService {
    private Handler postHandle = new Handler();
    private Context context;
    //    private long time = 3 * 60 * 1000;
    private long time =30 *60 * 1000;

    /**
     * @param context
     */
    public TimeoutService(Context context) {
        this.context = context;
    }

    public TimeoutService() {

    }
    /**
     * 按下移除计时器
     */
    public void stop() {
        postHandle.removeCallbacks(runnable);
    }
    /**
     * 启动计时器
     */
    public void start() {
        postHandle.removeCallbacks(runnable);
        postHandle.postDelayed(runnable, time);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {

            Intent logoutIntent = new Intent(context, PandoraEntryActivityLiunn.class);
            logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(logoutIntent);
        }
    };
}

 触屏事件参考了https://blog.csdn.net/zhixun2012/article/details/70207587 

          

猜你喜欢

转载自blog.csdn.net/liuno0/article/details/105856557
今日推荐