Android 将墙纸图像设置为锁屏背景技巧
**此代码将图像置于屏幕顶部,但隐藏所有锁屏内容,并禁用锁屏触摸 ** 要求 绘制的图像应显示为锁屏的背景 锁屏内容应可见 通知托盘不应受到阻碍 应启用锁屏触摸Android 将墙纸图像设置为锁屏背景技巧,android,lockscreen,android-windowmanager,Android,Lockscreen,Android Windowmanager,**此代码将图像置于屏幕顶部,但隐藏所有锁屏内容,并禁用锁屏触摸 ** 要求 绘制的图像应显示为锁屏的背景 锁屏内容应可见 通知托盘不应受到阻碍 应启用锁屏触摸 import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.WindowManager;
import android.widget.ImageView;
public class LockScreenImageService extends Service {
private BroadcastReceiver mReceiver;
private boolean isShowing = false;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private WindowManager windowManager;
private ImageView imageView;
WindowManager.LayoutParams params;
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
imageView = new ImageView(this);
imageView.setBackgroundColor(this.getResources().getColor(R.color.colorAccent));
imageView.setImageDrawable(this.getResources().getDrawable(R.drawable.ic_img));
//set parameters for the imageView
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER,
PixelFormat.TRANSPARENT);
//Register receiver for determining screen off and if user
// is present
mReceiver = new LockScreenStateReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
registerReceiver(mReceiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
public class LockScreenStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//if screen is turn off show the imageView
if (!isShowing) {
// windowManager.addView(imageView, params);
isShowing = true;
}
}
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
//Handle resuming events if user is present/screen is unlocked remove the imageView immediately
if (isShowing) {
windowManager.removeViewImmediate(imageView);
isShowing = false;
}
}
}
}
@Override
public void onDestroy() {
//unregister receiver when the service is destroy
if (mReceiver != null) {
unregisterReceiver(mReceiver);
}
//remove view if it is showing and the service is destroy
if (isShowing) {
windowManager.removeViewImmediate(imageView);
isShowing = false;
}
super.onDestroy();
}
}