随着android的版本越来越高,在手机后台存活率越来越低,该代码很好的应用了锁屏运行1px像素,广播监控,以及播放无声音乐等手段进行保活操作(借用大神作品)当点击返回退出时 就会提示正在进行跑步,保活app,如下图 package com.jiangdg.keepappalive;import android.content.Intent;import android.os.Bundle;import android.os.Process;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.util.Log;import android.view.KeyEvent;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import com.jiangdg.keepappalive.receiver.ScreenReceiverUtil;import com.jiangdg.keepappalive.service.DaemonService;import com.jiangdg.keepappalive.service.PlayerMusicService;import com.jiangdg.keepappalive.utils.Contants;import com.jiangdg.keepappalive.utils.HwPushManager;import com.jiangdg.keepappalive.utils.JobSchedulerManager;import com.jiangdg.keepappalive.utils.ScreenManager;import com.jiangdg.keepappalive.utils.SystemUtils;import java.util.Timer;import java.util.TimerTask;/** 运动界面,处理各种保活逻辑 * * Created by jianddongguo on 2017/7/7. * http://blog.csdn.net/andrexpert */public class SportsActivity extends AppCompatActivity { private static final String TAG = "SportsActivity"; private Toolbar mToolBar; private TextView mTvRunTime; private Button mBtnRun; private int timeSec; private int timeMin; private int timeHour; private Timer mRunTimer; private boolean isRunning; // 动态注册锁屏等广播 private ScreenReceiverUtil mScreenListener; // 1像素Activity管理类 private ScreenManager mScreenManager; // JobService,执行系统任务 private JobSchedulerManager mJobManager; // 华为推送管理类 private HwPushManager mHwPushManager; private ScreenReceiverUtil.SreenStateListener mScreenListenerer = new ScreenReceiverUtil.SreenStateListener() { @Override public void onSreenOn() { // 亮屏,移除"1像素" mScreenManager.finishActivity(); } @Override public void onSreenOff() { // 接到锁屏广播,将SportsActivity切换到可见模式 // "咕咚"、"乐动力"、"悦动圈"就是这么做滴// Intent intent = new Intent(SportsActivity.this,SportsActivity.class);// startActivity(intent); // 如果你觉得,直接跳出SportActivity很不爽 // 那么,我们就制造个"1像素"惨案 mScreenManager.startActivity(); } @Override public void onUserPresent() { // 解锁,暂不用,保留 } }; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sports); if(Contants.DEBUG) Log.d(TAG,"--->onCreate"); // 1. 注册锁屏广播监听器// mScreenListener = new ScreenReceiverUtil(this);// mScreenManager = ScreenManager.getScreenManagerInstance(this);// mScreenListener.setScreenReceiverListener(mScreenListenerer); // 2. 启动系统任务 mJobManager = JobSchedulerManager.getJobSchedulerInstance(this); mJobManager.startJobScheduler(); // 3. 华为推送保活,允许接收透传// mHwPushManager = HwPushManager.getInstance(this);// mHwPushManager.startRequestToken();// mHwPushManager.isEnableReceiveNormalMsg(true);// mHwPushManager.isEnableReceiverNotifyMsg(true); initView(); } private void initView() { mToolBar = (Toolbar)findViewById(R.id.toolbar_sports); mToolBar.setTitleTextColor(getResources().getColor(R.color.colorWhite)); mToolBar.setTitle("跑步啦"); mTvRunTime = (TextView)findViewById(R.id.tv_run_time); mBtnRun = (Button)findViewById(R.id.btn_run); } public void onRunningClick(View v){ if(! isRunning){ mBtnRun.setText("停止跑步"); startRunTimer(); // 3. 启动前台Service startDaemonService(); // 4. 启动播放音乐Service startPlayMusicService(); }else{ mBtnRun.setText("开始跑步"); stopRunTimer(); //关闭前台Service stopDaemonService(); //关闭启动播放音乐Service stopPlayMusicService(); } isRunning = !isRunning; } private void stopPlayMusicService() { Intent intent = new Intent(SportsActivity.this, PlayerMusicService.class); stopService(intent); } private void startPlayMusicService() { Intent intent = new Intent(SportsActivity.this,PlayerMusicService.class); startService(intent); } private void startDaemonService() { Intent intent = new Intent(SportsActivity.this, DaemonService.class); startService(intent); } private void stopDaemonService() { Intent intent = new Intent(SportsActivity.this, DaemonService.class); stopService(intent); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // 禁用返回键 if(keyCode == KeyEvent.KEYCODE_BACK){ if(isRunning){ Toast.makeText(SportsActivity.this,"正在跑步",Toast.LENGTH_SHORT).show(); return true; } } return super.onKeyDown(keyCode, event); } private void startRunTimer() { TimerTask mTask = new TimerTask() { @Override public void run() { timeSec ; if(timeSec == 60){ timeSec = 0; timeMin ; } if(timeMin == 60){ timeMin = 0; timeHour ; } if(timeHour == 24){ timeSec = 0; timeMin = 0; timeHour = 0; } // 更新UI runOnUiThread(new Runnable() { @Override public void run() { mTvRunTime.setText(timeHour " : " timeMin " : " timeSec); } }); } }; mRunTimer = new Timer(); // 每隔1s更新一下时间 mRunTimer.schedule(mTask,1000,1000); } private void stopRunTimer(){ if(mRunTimer != null){ mRunTimer.cancel(); mRunTimer = null; } timeSec = 0; timeMin = 0; timeHour = 0; mTvRunTime.setText(timeHour " : " timeMin " : " timeSec); } @Override protected void onDestroy() { super.onDestroy(); if(Contants.DEBUG) Log.d(TAG,"--->onDestroy"); stopRunTimer();// mScreenListener.stopScreenReceiverListener(); }}
下载android保活代码.zip用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论