自定义android程序一段时间无操作后的功能

项目中遇见一个这样的需求,就是当软件在一定时间没有操作时候需要弹出广告页面,当点击广告页面时又进行软件操作,也就是广告要在软件打开并且处于未操作状态才会出来。

方法一:用handler+onTouchEvent实现延时发送请求

这个操作最好放到BaseActivity里进行。

[代码]java代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private Handler handler = new Handler();
private long time=1000*60*5;
@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            handler.removeCallbacks(runnable);
            break;
        case MotionEvent.ACTION_UP:
            startAD();
            break;
    }
    return super.onTouchEvent(event);
}
private Runnable runnable = new Runnable() {
    @Override
    public void run() {
       跳转到广告页面
    }
};
public void startAD() {
    handler.removeCallbacks(runnable);
    handler.postDelayed(runnable, time);
}

方法二:通过PowerManager.WakeLock实现

判断屏幕是否变暗的这个思路去实现。

方法三:使用双线程来实现 (时间超时或者在接收到锁屏广播的时候显示)

可以在application或者activity的oncreated方法里面启动监听。

[代码]java代码:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
public class MainActivity extends Activity { 
   
    private Handler mHandler01 = new Handler(); 
    private Handler mHandler02 = new Handler(); 
       
    /* 上一次User有动作的Time Stamp */ 
    private Date lastUpdateTime; 
    /* 计算User有几秒没有动作的 */ 
    private long timePeriod; 
       
    /* 静止超过N秒将自动进入屏保 */ 
    private float mHoldStillTime = 10; 
    /*标识当前是否进入了屏保*/ 
    private boolean isRunScreenSaver; 
       
    /*时间间隔*/ 
    private long intervalScreenSaver = 1000; 
    private long intervalKeypadeSaver = 1000; 
       
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        ((Button)findViewById(R.id.start_screen_Saver)).setOnClickListener(new View.OnClickListener() { 
               
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub 
                Intent mService = new Intent(MainActivity.this, ScreenService.class); 
                mService.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                startService(mService); 
            
        }); 
           
        /* 初始取得User可触碰屏幕的时间 */ 
        lastUpdateTime = new Date(System.currentTimeMillis()); 
    
       
    /**
     * 计时线程
     */ 
    private Runnable mTask01 = new Runnable() { 
           
        @Override 
        public void run() { 
            Date timeNow = new Date(System.currentTimeMillis()); 
            /* 计算User静止不动作的时间间距 */ 
            /**当前的系统时间 - 上次触摸屏幕的时间 = 静止不动的时间**/ 
            timePeriod = (long) timeNow.getTime() - (long) lastUpdateTime.getTime(); 
               
            /*将静止时间毫秒换算成秒*/ 
            float timePeriodSecond = ((float) timePeriod / 1000); 
               
            if(timePeriodSecond > mHoldStillTime){ 
                if(isRunScreenSaver == false){  //说明没有进入屏保 
                    /* 启动线程去显示屏保 */ 
                    mHandler02.postAtTime(mTask02, intervalScreenSaver); 
                    /*显示屏保置为true*/ 
                    isRunScreenSaver = true; 
                }else{ 
                    /*屏保正在显示中*/ 
                
            }else{ 
                /*说明静止之间没有超过规定时长*/ 
                isRunScreenSaver = false; 
            
            /*反复调用自己进行检查*/ 
            mHandler01.postDelayed(mTask01, intervalKeypadeSaver);   
        
    }; 
       
    /**
     * 持续屏保显示线程
     */ 
    private Runnable mTask02 = new Runnable() { 
   
        @Override 
        public void run() { 
            // TODO Auto-generated method stub 
            if (isRunScreenSaver == true) {  //如果屏保正在显示,就计算不断持续显示   
//              hideOriginalLayout(); 
                showScreenSaver(); 
                mHandler02.postDelayed(mTask02, intervalScreenSaver); 
            } else { 
                mHandler02.removeCallbacks(mTask02);  //如果屏保没有显示则移除线程 
            
        
    }; 
       
    /**
     * 显示屏保
     */ 
    private void showScreenSaver(){ 
        Log.d("danxx", "显示屏保------>"); 
        Intent intent = new Intent(MainActivity.this, ScreenSaverActivity.class); 
        startActivity(intent); 
           
    
       
    @Override 
    public boolean dispatchKeyEvent(KeyEvent event) { 
        updateUserActionTime(); 
        return super.dispatchKeyEvent(event); 
    
       
    /*用户有操作的时候不断重置静止时间和上次操作的时间*/ 
    public void updateUserActionTime() { 
        Date timeNow = new Date(System.currentTimeMillis()); 
        timePeriod = timeNow.getTime() - lastUpdateTime.getTime(); 
        lastUpdateTime.setTime(timeNow.getTime()); 
    
       
    @Override 
    protected void onResume() { 
        /*activity显示的时候启动线程*/ 
        mHandler01.postAtTime(mTask01, intervalKeypadeSaver); 
        super.onResume(); 
    
       
    @Override 
    protected void onPause() { 
        /*activity不可见的时候取消线程*/ 
        mHandler01.removeCallbacks(mTask01); 
        mHandler02.removeCallbacks(mTask02); 
        super.onPause(); 
    
       
}

下面是启动一个服务,当用户按锁屏键锁屏的时候会发送一个广播,我们在服务中接受到广播后可以屏蔽系统的锁屏,显示我们自己的锁屏:

[代码]java代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class ScreenService extends Service { 
    KeyguardManager mKeyguardManager = null;    
    private KeyguardLock mKeyguardLock = null;   
    BroadcastReceiver mMasterResetReciever; 
    @Override 
    public IBinder onBind(Intent intent) { 
        // TODO Auto-generated method stub 
        return null
    
     @Override    
        public void onCreate() {  
             //Log.e("ScreenService","onCreate()"); 
            // TODO Auto-generated method stub  
            startScreenService(); 
            super.onCreate();    
          }    
           
     @Override    
     public void onStart(Intent intent, int startId) {   
        // Log.e("ScreenService","onStart"); 
        // TODO Auto-generated method stub    
         startScreenService(); 
      
            
     private void startScreenService(){ 
         mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);    
         mKeyguardLock = mKeyguardManager.newKeyguardLock(""); 
            
            
         mKeyguardLock.disableKeyguard();    
            
         //Intent.ACTION_SCREEN_OFF 
         mMasterResetReciever = new BroadcastReceiver() {    
             @Override 
                public void onReceive(Context context, Intent intent) {    
                 try {  
                         Intent i = new Intent();    
                         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
                         i.setClass(context, ScreenSaverActivity.class);                         
                         context.startActivity(i);  
                 } catch (Exception e) {                         
                     Log.i("mMasterResetReciever:", e.toString());    
                 }    
             }    
         };            
       registerReceiver(mMasterResetReciever, new IntentFilter(Intent.ACTION_SCREEN_OFF));    
     
        
     @Override 
    public void onDestroy() { 
        //Log.e("ScreenService","onDestroy()"); 
        super.onDestroy(); 
        unregisterReceiver(mMasterResetReciever);   
         ScreenService.this.stopSelf(); 
     
}
上一篇:mybatis一对一关联查询——(八)


下一篇:微信小程序——获取当天的前一个月至后一个月