问题一:监听手机屏幕 的状态的改变
问题二:屏蔽手机自带的密码锁
问题三:如何屏蔽home键
问题
public class ScreenListener {
private Context mContext;
private ScreenBroadcastReceiver mScreenReceiver;
private ScreenStateListener mScreenStateListener;
public ScreenListener(Context context) {
mContext = context;
mScreenReceiver = new ScreenBroadcastReceiver();
}
private class ScreenBroadcastReceiver extends BroadcastReceiver {
private String action = null;
@Override
public void onReceive(Context context, Intent intent) {
action = intent.getAction();
if (Intent.ACTION_SCREEN_ON.equals(action)) {
mScreenStateListener.onScreenOn();
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
mScreenStateListener.onScreenOff();
} else if (Intent.ACTION_USER_PRESENT.equals(action)) {
mScreenStateListener.onUserPresent();
}
}
}
public void begin(ScreenStateListener listener) {
mScreenStateListener = listener;
registerListener();
getScreenState();
}
private void getScreenState() {
PowerManager manager = (PowerManager) mContext
.getSystemService(Context.POWER_SERVICE);
if (manager.isScreenOn()) {
if (mScreenStateListener != null) {
mScreenStateListener.onScreenOn();
}
} else {
if (mScreenStateListener != null) {
mScreenStateListener.onScreenOff();
}
}
}
public void unregisterListener() {
mContext.unregisterReceiver(mScreenReceiver);
}
private void registerListener() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
mContext.registerReceiver(mScreenReceiver, filter);
}
public interface ScreenStateListener {
public void onScreenOn(); //屏幕亮
public void onScreenOff(); //屏幕黑
public void onUserPresent(); //用户解锁
}
}
这是监听屏幕状态改变的工具类
我们用一个服务来工作,使用工具类
ScreenListener listener=new ScreenListener(HomeService.this);
listener.begin(this);
public class HomeService extends Service implements ScreenStateListener{
private ScreenListener listener;
private WindowManager mWinMng; //窗口管理器
private MainView mainView; //自定义View
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
listener=new ScreenListener(HomeService.this);
listener.begin(this);
//这么做是为啦 提升服务的优先级 使他变成前台的优先级 不然老死
startForeground(1, getNotifion());
return super.onStartCommand(intent, flags, startId);
}
private Notification getNotifion() {
Intent intent=new Intent(this,HomeService.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pintent=PendingIntent.getActivity(this, 0,intent , 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
RemoteViews myNotificationView = new RemoteViews(getPackageName(), R.layout.notification_view);
myNotificationView.setTextViewText(R.id.notification_title, "锁屏正在运行中!");
myNotificationView.setTextViewText(R.id.notification_text,"锁屏正在运行中!");
myNotificationView.setImageViewBitmap(R.id.notification_large_icon, getIco());
builder.setContent(myNotificationView);
builder.setContentTitle("锁屏正在运行中")
.setContentText("锁屏正在运行中")
.setContentIntent(pintent)
.setAutoCancel(true);
Notification mNotification = builder.build();
mNotification.contentView = myNotificationView;
return mNotification;
}
private Bitmap getIco() {
BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.ic_launcher);
return bd.getBitmap();
}
@Override
public void onDestroy() {
//解除注册
listener.unregisterListener();
super.onDestroy();
}
@Override
public void onScreenOn() {
//获取窗口管理器对象
mWinMng = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
//屏蔽系统锁屏
KeyguardManager mKeyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock mKeyguardLock = mKeyguardManager.newKeyguardLock("zdLock 1");
mKeyguardLock.disableKeyguard();
//需要悬浮窗权限
addView();
}
@Override
public void onScreenOff() {
}
@Override
public void onUserPresent() {
isup=true;
}
public void addView()
{
//在窗口上添加View 因为只是单纯的View 所以Home键无效
mainView = new MainView(getApplicationContext(),mWinMng,HomeService.this);
LayoutParams param = new LayoutParams();
param.type = LayoutParams.TYPE_SYSTEM_ALERT;//悬浮窗口,在其他程序的顶层
param.format = PixelFormat.RGBA_8888;
param.flags =WindowManager.LayoutParams.FLAG_FULLSCREEN;
param.width =LayoutParams.MATCH_PARENT;
param.height = LayoutParams.MATCH_PARENT;
mWinMng.addView(mainView, param);
}
}
代码注释的很详细,效果还好
希望对大家有帮助