qq的后台服务,在我们按2下返回键,退出应用app后,消息来了,会突破锁屏,点亮屏幕,看见消息弹出框,后面可以进行一系列的操作。qq的服务是不会被360杀死的,是属于我们所说的流氓软件,会在后台偷跑流量的,只有用户手动在应用程序里面关闭才行的。我所写的demo,暂时不会有这个流氓软件的功能的,以后如果有业务需求,也许会加上的,关于屏幕解锁,不同的手机的解锁方式是不同的迷宫解锁,密码解锁...但是系统解锁都是可以解的。还有就是因为急着出版本,所以这一版本暂时没有突破锁屏过后的弹出框,这个功能在下一版本会有的,我会继续更新的。废话也说了这么多了,上代码吧。
业务需求:
1.有个界面,上面有这几个元素:
停止按钮
开始按钮
复位按钮
进度条0%-100%
2.后台有个服务,包含以下逻辑:
有个进度值,初始是0%,最小是0%,最大是100%
初始状态是停止的,什么都不做
收到开始命令后,每秒增加1%的进度
收到停止命令后,停下进度增长,进度维持目前的数值,什么都不做
收到复位命令后,将进度调整为0%
3.界面与服务的协作逻辑表达为用例如下:
打开界面界面上什么都不动,进度条为0
点开始,服务开始按内在逻辑走进度,并通知界面更新进度条控件
点停止,服务中的进度逻辑停止
如果在进度走到20%的时候,退出界面(Home键、返回、熄屏、返回+熄屏),等待10秒再进入,应看到进度条处在30%的地方,且仍旧在走
停止时点复位,界面中的进度条应瞬间回到0%,且停留在0%
进行时点复位,界面中的进度条应瞬间回到0%,且继续走
<span style="font-size:14px;"><span style="font-size:12px;">package com.example.localservice;
import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener, Ctroller {
private static final String TAG = "MainActivity";
private ProgressBar progressBar;
private Button startBtn;
private Button stopBtn;
private Button resetBtn;
private TextView tx;
private Intent intent = new Intent();
private UpdateReceiver updateReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//动态注册广播接收器
updateReceiver = new UpdateReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.localservice.UPDATE");
registerReceiver(updateReceiver, intentFilter);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
startBtn = (Button) findViewById(R.id.start);
stopBtn = (Button) findViewById(R.id.stop);
resetBtn = (Button) findViewById(R.id.reset);
tx = (TextView) findViewById(R.id.tx);
startBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
resetBtn.setOnClickListener(this);
Intent in = new Intent(this, MyService.class);
startService(in);
//开机发送广播,拿数据
intent.setAction("com.example.localservice.NOTIFICATION");
intent.putExtra("click", "data");
sendBroadcast(intent);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
intent.setAction("com.example.localservice.NOTIFICATION");
intent.putExtra("click", "start");
sendBroadcast(intent);
break;
case R.id.stop:
intent.setAction("com.example.localservice.NOTIFICATION");
intent.putExtra("click", "stop");
sendBroadcast(intent);
break;
case R.id.reset:
intent.setAction("com.example.localservice.NOTIFICATION");
intent.putExtra("click", "reset");
sendBroadcast(intent);
break;
}
}
public class UpdateReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//拿到进度,更新UI
int progress = intent.getIntExtra("progress", 0);
progressBar.setProgress(progress);
tx.setText(progress + "%");
if(progress % 10 == 0){
SoundManager.getInstance(context).playSound(SoundManager.NETERROR);
}else{
SoundManager.getInstance(context).stopSound();
}
if(progress % 20 == 0){
lighten();
}else {
}
}
}
@Override
protected void onPause() {
super.onPause();
MyService.IS_DESTORY = false;
}
@Override
protected void onResume() {
super.onResume();
MyService.IS_DESTORY = false;
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(updateReceiver);
SoundManager.getInstance(this).stopSound();
MyService.IS_DESTORY = true;
Log.i(TAG, "------>>onDestroy");
}
/**
* 点亮屏幕
*/
public void lighten() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!pm.isScreenOn()) {
// 获取电源管理器对象
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
// 点亮屏幕
wl.acquire();
if (!MyService.IS_DESTORY) {
// 解锁
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
} else {
// 获取电源管理器对象
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
// 得到键盘锁管理器对象
KeyguardLock kl = km.newKeyguardLock("unLock");
// 解锁
kl.disableKeyguard();
// kl.reenableKeyguard();
// 重新启用自动加锁
// wl.release();
// 释放
}
}
}
@Override
public void dismissKeyguard() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
@Override
public void runOnUI(Runnable run) {
runOnUiThread(run);
}
}</span></span>
<span style="font-size:14px;"><span style="font-size:12px;">package com.example.localservice;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.WindowManager;
public class MyService extends Service {
public static final int MAX_PROGRESS = 100;
private int progress = 0;
private ServiceReciver serviceReciver;
private int ok = 0;
public static boolean IS_DESTORY = false;
private Ctroller object;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
// 动态注册广播接收器
serviceReciver = new ServiceReciver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.localservice.NOTIFICATION");
registerReceiver(serviceReciver, intentFilter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(serviceReciver);
}
/**
* 模拟下载任务,每秒钟更新一次
*/
public void startDownLoad() {
new Thread(new Runnable() {
@Override
public void run() {
while (ok < MAX_PROGRESS) {
ok += 1;
progress += 1;
// 发送Action为com.example.localservice.UPDATE的广播
if(IS_DESTORY){
Intent intent = new Intent(MyService.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Intent in = new Intent();
in.putExtra("progress", progress);
in.setAction("com.example.localservice.UPDATE");
sendBroadcast(in);
}else {
Intent in = new Intent();
in.putExtra("progress", progress);
in.setAction("com.example.localservice.UPDATE");
sendBroadcast(in);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// if (progress % 20 == 0) {
// lighten();
// setBrightness();
// }
}
}
}).start();
}
public void stop() {
Intent in = new Intent();
in.putExtra("progress", progress);
in.setAction("com.example.localservice.UPDATE");
sendBroadcast(in);
}
public void reset() {
progress = 0;
Intent in = new Intent();
in.putExtra("progress", progress);
in.setAction("com.example.localservice.UPDATE");
sendBroadcast(in);
}
public class ServiceReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String extra = intent.getStringExtra("click");
if ("data".equals(extra)) {
Intent in = new Intent();
in.putExtra("progress", progress);
in.setAction("com.example.localservice.UPDATE");
sendBroadcast(in);
}
if ("start".equals(extra)) {
// myThread.start();
ok = progress;
startDownLoad();
}
if ("stop".equals(extra)) {
ok = 101;
stop();
}
if ("reset".equals(extra)) {
reset();
}
}
}
// // 改变亮度
// public static void SetLightness(Activity act, int value) {
// try {
// System.putInt(act.getContentResolver(), System.SCREEN_BRIGHTNESS,
// value);
// WindowManager.LayoutParams lp = act.getWindow().getAttributes();
// lp.screenBrightness = (value <= 0 ? 1 : value) / 255f;
// act.getWindow().setAttributes(lp);
// } catch (Exception e) {
// Toast.makeText(act, "无法改变亮度", Toast.LENGTH_SHORT).show();
// }
// }
//
// // 获取亮度
// public static int GetLightness(Activity act) {
// return System.getInt(act.getContentResolver(),
// System.SCREEN_BRIGHTNESS, -1);
// }
/**
* 点亮屏幕
*/
public void lighten() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!pm.isScreenOn()) {
// 获取电源管理器对象
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
// 点亮屏幕
wl.acquire();
if (!IS_DESTORY) {
// 解锁
// kl.disableKeyguard();
} else {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// 获取电源管理器对象
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
// 得到键盘锁管理器对象
KeyguardLock kl = km.newKeyguardLock("unLock");
// 解锁
kl.disableKeyguard();
// kl.reenableKeyguard();
// 重新启用自动加锁
// wl.release();
// 释放
}
}
}
/**
* 改变屏幕亮度
*/
protected void setBrightness() {
WindowManager.LayoutParams lp = ((MainActivity) object).getWindow().getAttributes();
int brightness = Settings.System.getInt(((MainActivity) object).getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 0);
// if(brightness <217)
// lp.screenBrightness = 0.85f;
// else
// brightness += 10;
// if(brightness > 255)
// brightness = 60;
lp.screenBrightness = brightness / 255.0f;
((MainActivity) object).getWindow().setAttributes(lp);
Settings.System.putInt(((MainActivity) object).getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, brightness);
}
}
</span></span>
后面增加新的需求,如果进度条跑到10.20.30.40.50.60.70.80,90.100,会发出提示声音。如果进度条跑到20.40.60.80.100,会点亮屏幕,解锁。其实在updateReceiver中的<span style="font-size:14px;"><span style="font-size:12px;">if(progress % 10 == 0){
SoundManager.getInstance(context).playSound(SoundManager.NETERROR);
}else{
SoundManager.getInstance(context).stopSound();
}
if(progress % 20 == 0){
lighten();
}else {
}</span></span>
应该放在服务中更好,我比较懒,在demo中就没改了,好了,暂时就这么多了,等下一版本吧。下载地址:http://download.youkuaiyun.com/detail/u012301841/8250881