我正在开发锁屏。我成功地完成了所有的工作,但现在当我锁定屏幕时我的锁屏第一次解锁并再次锁定时,我的锁屏无法正常工作。
我无法找到背后的原因。
我的锁屏服务
public class LockscreenService extends Service {
private LinearLayout linearLayout;
private WindowManager.LayoutParams layoutParams;
private WindowManager windowManager;
public static final String mypreference="mypref";
public static final String name4 ="key";
int imname;
SharedPreferences sp;
ImageView img,aimg;
final ArrayList<Bitmap> beforeshuffle = new ArrayList<>(9);
final ArrayList<Bitmap> aftershuffle = new ArrayList<>(9);
ArrayList<Bitmap> smallimages = new ArrayList<Bitmap>(9);
public static final String name1 ="imgkey";
GridView grid;
Bitmap bs;
Bitmap as;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
BroadcastReceiver screenReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean b = sp.getBoolean(name4, false);
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) ||intent.getAction().equals(Intent.ACTION_SCREEN_ON) && linearLayout == null) {
init();
}
}
};
@Override
public void onCreate() {
super.onCreate();
sp = getSharedPreferences(mypreference, Context.MODE_PRIVATE);
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(screenReceiver, intentFilter);
windowManager = ((WindowManager) getSystemService(WINDOW_SERVICE));
layoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION,
PixelFormat.TRANSLUCENT);
}
private void init() {
if (sp.contains(name1)) {
imname = sp.getInt(name1, 0);
}
linearLayout = new LinearLayout(this);
windowManager.addView(linearLayout, layoutParams);
((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.lockscreen, linearLayout);
// View btnClose = linearLayout.findViewById(R.id.button);
String wp="/data/user/0/com.example.user.myapplication/app_wallparerdir/wall.jpg";
Bitmap my = BitmapFactory.decodeFile(wp);
Drawable d = new BitmapDrawable(getResources(), my);
if(imname==1)
{
linearLayout.setBackground(d);
}
else {
//Drawable image = c.getResources().getDrawable(R.drawable.name_of_image_in_drawableFolder);
linearLayout.setBackgroundResource(imname);
}
img = (ImageView) linearLayout.findViewById(R.id.bs);
aimg = (ImageView) linearLayout.findViewById(R.id.as);
grid = (GridView) linearLayout.findViewById(R.id.gridView);
Globalvariable c=new Globalvariable();
bs=c.giveBitmap();
String p="/data/user/0/com.example.user.myapplication/app_imageDir/profile.jpg";
Toast.makeText(getApplication(),p,Toast.LENGTH_SHORT).show();
Log.d("path",p);
String bspath="/data/user/0/com.example.user.myapplication/app_Beforeshuffle/beforeshuffle";
bs=loadImageFromStorage(bspath);
// Bitmap bitmap = (Bitmap) i.getParcelableExtra("bitmap");
//smallimage_Numbers is to tell how many smallimage_s the image should split
int smallimage_Numbers = 9;
//Getting the source image to split
// ImageView image = (ImageView) findViewById(R.id.source_image);
// File imgFile = new File("profile.jpg"
Bitmap myBitmap = BitmapFactory.decodeFile(p);
splitImage(myBitmap, smallimage_Numbers);
View btnClose = linearLayout.findViewById(R.id.button8);
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img.setImageBitmap(bs);
as=getBitmapFromView(grid);
aimg.setImageBitmap(as);
Bitmap a=getBitmapFromView(img);
Bitmap b=getBitmapFromView(aimg);
img.setVisibility(View.INVISIBLE);
aimg.setVisibility(View.INVISIBLE);
// do something when the corky3 is clicked
if(b.sameAs(a) ){
// Toast.makeText(getApplicationContext(),"CORRECT",Toast.LENGTH_SHORT).show();
///Intent service = new Intent(LockscreenService.this, calculator.class);
// startService(service);
Intent service = new Intent(LockscreenService.this, caclservice.class);
startService(service);
// windowManager.removeView(linearLayout);
// linearLayout = null;
}
else
{
Log.d("jhgeiugh","jhgfeug");
// Toast.makeText(getApplicationContext(),"WRONG",Toast.LENGTH_SHORT).show();
}
}
});
View btnemergency = linearLayout.findViewById(R.id.button6);
btnemergency.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("ghfgh","dgd");
Intent service = new Intent(LockscreenService.this, PopupService.class);
startService(service);
// Toast.makeText(getApplicationContext(),"setted",Toast.LENGTH_SHORT).show();
}
});
View btnforgot = linearLayout.findViewById(R.id.button7);
btnforgot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("ghfgh","dgd");
Intent service = new Intent(LockscreenService.this, Forgotpassservice.class);
startService(service);
// Toast.makeText(getApplicationContext(),"setted",Toast.LENGTH_SHORT).show();
}
});
}
我的锁屏显示要解决的图片拼图然后计算器输入pin no来解锁设备。所以计算器服务
public class caclservice extends Service {
private int[] numericButtons = {R.id.btnZero, R.id.btnOne, R.id.btnTwo, R.id.btnThree, R.id.btnFour, R.id.btnFive, R.id.btnSix, R.id.btnSeven, R.id.btnEight, R.id.btnNine};
private static final String TAG = PopupService.class.getSimpleName();
WindowManager mWindowManager;
QuestionAdapter qa;
View mView; String username;
TextView ques;
private Session session;
String question;
private TextView txtScreen;
// Represent whether the lastly pressed key is numeric or not
private boolean lastNumeric;
// Represent that current state is in error or not
private boolean stateError=false;
// If true, do not allow to add another DOT
private boolean lastDot;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
stateError=false;
showDialog();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
session=new Session(this);
qa=new QuestionAdapter(this);
username=session.getusename();
question=qa.fetchreco(username);
setNumericOnClickListener();
return START_STICKY;
//return super.onStartCommand(intent, flags, startId);
}
private void showDialog() {
// PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
// PowerManager.WakeLock mWakeLock = pm.newWakeLock((PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "caclservice");
// mWakeLock.acquire();
// mWakeLock.release();
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mView = View.inflate(getApplicationContext(), R.layout.calculator, null);
mView.setTag(TAG);
int top = getApplicationContext().getResources().getDisplayMetrics().heightPixels / 2;
LinearLayout dialog = (LinearLayout) mView.findViewById(R.id.lin);
// if you want to set params
// android.widget.LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) dialog.getLayoutParams();
// lp.topMargin = top;
// lp.bottomMargin = top;
// mView.setLayoutParams(lp);
txtScreen = (TextView) mView.findViewById(R.id.txtScreen);
// ques.setText(question);
// final EditText etMassage = (EditText) mView.findViewById(R.id.ans);
//etMassage.setText("");
/* ImageButton imageButtonSend = (ImageButton) mView.findViewById(R.id.imageButtonSendInPopupMessageReceived);
// lp = (LayoutParams) imageButton.getLayoutParams();
// lp.topMargin = top - 58;
// imageButton.setLayoutParams(lp);
imageButtonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!etMassage.getText().toString().equals(""))
{
etMassage.setText("");
}
}
});*/
Button cancel = (Button) mView.findViewById(R.id.cancel);
// close.setText("Cancel");
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
hidethisDialog();
}
});
Button close = (Button) mView.findViewById(R.id.btnEqual);
// close.setText("Cancel");
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String ans = txtScreen.getText().toString();
if (ans.trim().equals(question.trim())) {
stateError=true;
hideDialog();
// int flags = WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
// getWindow().addFlags(flags);
}
}
});
final WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
// | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
PixelFormat.RGBA_8888);
mView.setVisibility(View.VISIBLE);
mWindowManager.addView(mView, mLayoutParams);
mWindowManager.updateViewLayout(mView, mLayoutParams);
}
private void setNumericOnClickListener() {
// Create a common OnClickListener
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// Just append/set the text of clicked button
Button button = (Button) v;
if (stateError) {
// If current state is Error, replace the error message
txtScreen.setText(button.getText());
stateError = false;
} else {
// If not, already there is a valid expression so append to it
txtScreen.append(button.getText());
}
// Set the flag
lastNumeric = true;
}
};
// Assign the listener to all the numeric buttons
for (int id : numericButtons) {
mView.findViewById(id).setOnClickListener(listener);
}
}
private void hidethisDialog(){
if(mView != null && mWindowManager != null){
mWindowManager.removeView(mView);
mView = null;
}
}
private void hideDialog(){
if(mView != null && mWindowManager != null){
mWindowManager.removeView(mView);
mView = null;
startService(new Intent(this, LockscreenService.class));
/*
* Notify the system to finalize and collect all objects of the
* application on exit so that the process running the application can
* be killed by the system without causing issues. NOTE: If this is set
* to true then the process will not be killed until all of its threads
* have closed.
*/
System.runFinalizersOnExit(true);
/*
* Force the system to close the application down completely instead of
* retaining it in the background. The process that runs the application
* will be killed. The application will be completely created as a new
* application in a new process if the user starts the application
* again.
*/
System.exit(0);
// stopService(new Intent(ActivityName.this, ServiceClassName.class));
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
我在活动中将其调用以锁定屏幕
Intent service = new Intent(getApplicationContext(), LockscreenService.class);
getApplication().startService(service);
答案 0 :(得分:0)
如果广播接收器不工作,那么您可以制作连续运行背景并观看该屏幕已关闭/开启的服务。
像这样:PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
isScreenOn = pm.isScreenOn();
if (isScreenOn) {
//Screen is on
}
else
{
//Screen is off
}