如何在取消选中我的复选框时禁用我的锁屏

时间:2017-11-18 06:26:44

标签: android lockscreen

我已成功实现我的锁屏与拼图图像它完全没问题但是现在我想在用户取消选中此复选框时禁用锁屏并启用如果用户选中复选框 说我怎么做 我的锁屏代码

 public class LockscreenService extends Service {
private LinearLayout linearLayout;
private WindowManager.LayoutParams layoutParams;
private WindowManager windowManager;
String p;
ImageView img,aimg;
final ArrayList<Bitmap> beforeshuffle = new ArrayList<>(9);
final ArrayList<Bitmap> aftershuffle = new ArrayList<>(9);
ArrayList<Bitmap> smallimages = new ArrayList<Bitmap>(9);
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) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) && linearLayout == null) {
            init();
        }
    }
};
@Override
public void onCreate() {
    super.onCreate();
    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() {
    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);
    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();
            }

        }
    });

我锁定了我my checknox in lockscreen menu nu

在此图片中有复选框,我需要知道用户点击它时我必须做什么

具有共享偏好的代码

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    sharedPreferences =getSharedPreferences(mypreference, Context.MODE_PRIVATE);
    final IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_OFF);
CustomList adapter = new
        CustomList(this, web, imageId);
list=(ListView)findViewById(R.id.List);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        final CheckBox cb = (CheckBox) view.findViewById(R.id.cb);
        if (position == 1) {
            startActivity(new Intent(getApplicationContext(), ChangePassword.class));
        } else if (position == 2) {
            startActivity(new Intent(getApplicationContext(), Changephoto.class));
        } else if (position == 4) {
            startActivity(new Intent(getApplicationContext(), Puzzle.class));
        } else if (position == 5) {
            startActivity(new Intent(getApplicationContext(), choosephoto.class));
        }



    }
});

    cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

            if(isChecked){
                Log.d("ghjk", "ghfu");
                sharedPreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(name1, "true");
                editor.commit();
                cb.setChecked(true);
                KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
                KeyguardManager.KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
                lock.reenableKeyguard();
            }else {
                sharedPreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(name1, "false");
                editor.commit();
                KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
                KeyguardManager.KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
                lock.reenableKeyguard();
            }
        }
    });

    if(sharedPreferences.contains(name1)){
        String b=sharedPreferences.getString(name1,"");
        Log.d("dsf",b);
        if(b.equals("true")) {
           cb.setChecked(true);
        }else
        {
            cb.setChecked(false);
        }
    }

1 个答案:

答案 0 :(得分:0)

我解决了问题,因为@ashwin在复选框中存储了一个标志并检查了该标志值以取消注册这样的广播接收器

    BroadcastReceiver screenReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean b = sp.getBoolean(name4, false);
        if(b)
        {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) && linearLayout == null) {
                init();
            }

        }else
            unregisterReceiver(screenReceiver);

    }
};