如何将墙纸设置为锁屏或同时设置(家庭和锁)?

时间:2019-11-30 08:50:11

标签: android android-alertdialog android-wallpaper

我已经实现了“将墙纸设置为主屏幕”的实现。但是我没有得到如何将墙纸设置为锁定屏幕和(主屏幕和锁定)两者。在以下代码中,“如果”部分用于设置为主屏幕。是用于“锁定”和“同时(家庭和锁定)” ..将墙纸设置为锁定并且同时将墙纸设置为“家庭和锁定”的逻辑是什么??

代码:

public class ViewImage extends AppCompatActivity {
Button button1, button2;
public   List<RetroPhoto> product_lists=new ArrayList<>();
private RecyclerView recyclerView;
WallpaperManager myWallpaperManager;

public static FavoriteDatabase favoriteDatabase;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewimage_layout);

    ImageView mPlace = findViewById(R.id.imagewall);
    button1 = findViewById(R.id.button1);
    button2 = findViewById(R.id.button2);
    ImageButton back = (ImageButton) findViewById(R.id.backToMain);
    ImageButton downloadimage = findViewById(R.id.downloadimg);
    ImageView favimg = findViewById(R.id.favimg);
    recyclerView = findViewById(R.id.rec);
    favoriteDatabase = Room.databaseBuilder(getApplicationContext(), FavoriteDatabase.class, "myfavdb").allowMainThreadQueries().build();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    assert navigationView != null;
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    final Bundle bundle = this.getIntent().getExtras();

    final String imgUrl = bundle.getString("Title");
    Glide.with(this).load(imgUrl)
            .thumbnail(0.5f)
            .crossFade()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(mPlace);

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });



   button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           AlertDialog.Builder builder = new AlertDialog.Builder(ViewImage.this);

            // Set the alert dialog title
            builder.setTitle("Set Wallpaper");

            // Initialize a new string array of flowers
            final String[] flowers = new String[]{
                    "Home Screen",
                    "Lock screen",
                    "Both"
            };

            // Set a list for alert dialog
            builder.setItems(
                    flowers, // Items array
                    new DialogInterface.OnClickListener() // Item click listener
                    {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            // Get the alert dialog selected item's text
                            String selectedItem = Arrays.asList(flowers).get(i);
                           if(selectedItem.equals(flowers[0])) {
                               Glide.with(ViewImage.this)
                                       .load(imgUrl)
                                       .asBitmap()
                                       .into(new SimpleTarget<Bitmap>() {
                                           @Override
                                           public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                               try {
                                                   WallpaperManager.getInstance(getApplicationContext()).setBitmap(resource);

                                                   Toast.makeText(ViewImage.this, "successfully set as home wallpaper", Toast.LENGTH_LONG).show();
                                               } catch (IOException e) {
                                                   e.printStackTrace();
                                               }

                                           }


                                       });

                           }
                           else if(selectedItem.equals(flowers[1]))
                            {
                                Uri bitmap = MediaStore.Images.Media.getContentUri(imgUrl);
                                final Uri finalBitmap = bitmap;

                                try {
                                    WallpaperManager.getInstance(getApplicationContext()).setBitmap(finalBitmap,null,false,WallpaperManager.FLAG_LOCK);
                                 //   myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_LOCK);
                                    Toast.makeText(ViewImage.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                //Toast.makeText(ViewImage.this, "successfully set as Lock wallpaper", Toast.LENGTH_LONG).show();

                            }
                           else
                           {
                               Toast.makeText(ViewImage.this, "successfully set as both", Toast.LENGTH_LONG).show();

                           }

                        }
                    });

            // Create the alert dialog
            AlertDialog dialog = builder.create();

            // Get the alert dialog ListView instance
            ListView listView = dialog.getListView();

            // Set the divider color of alert dialog list view
            listView.setDivider(new ColorDrawable(Color.RED));

            // Set the divider height of alert dialog list view
            listView.setDividerHeight(0);

            // Finally, display the alert dialog
            dialog.show();
        }
   });



}
public Bitmap StringToBitMap(String encodedString){
    try {
        byte [] encodeByte= Base64.decode(encodedString,Base64.DEFAULT);
        Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
        return bitmap;
    } catch(Exception e) {
        e.getMessage();
        return null;
    }
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        finish(); // close this activity and return to preview activity (if there is any)
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
}

}

1 个答案:

答案 0 :(得分:1)

检查我的代码是否对您有用:)

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    ArrayList<String> input = new ArrayList<String>();

    while(!(sc.next().equals("#"))) {
        String s = sc.next();
        input.add(s);
    }
    System.out.println(input);
}

在OnCreate()

WallpaperManager myWallpaperManager;
private static final int CHOOSE_IMAGE = 22;
private Button btnSetWallpaper;
String[] options = new String[]{
 "Home Screen",
 "Lock Screen",
 "Both"
};

在OnActivityResult()

 btnSetWallpaper = findViewById(R.id.btnSetWallpaper);
        btnSetWallpaper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "choose image"), CHOOSE_IMAGE);

            }
        });

您只想设置标志以设置各种墙纸选项。 像...这样的标志

if (requestCode == CHOOSE_IMAGE && data != null) {
        mCropImageUri = data.getData();
        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mCropImageUri);
            Log.e(TAG, "onActivityResult: BIT " + bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Choose from below");
        final Bitmap finalBitmap = bitmap;
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String selectedItem = Arrays.asList(options).get(i);
                if (selectedItem.equals(options[0])) {
                    try {
                        myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
                        myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_SYSTEM);
                        Toast.makeText(MainActivity.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    } catch (Exception e) {
                        Log.e(TAG, "onResourceReady: " + e.getMessage());
                    }
                } else if (selectedItem.equals(options[1])) {
                    try {
                        myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
                        myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_LOCK);
                        Toast.makeText(MainActivity.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    } catch (Exception e) {
                        Log.e(TAG, "onResourceReady: " + e.getMessage());
                    }

                } else if (selectedItem.equals(options[2])) {
                    try {
                        myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
                        myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM);
                        Toast.makeText(MainActivity.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    } catch (Exception e) {
                        Log.e(TAG, "onResourceReady: " + e.getMessage());
                    }
                }
            }
        });
        dialog = builder.create();
        dialog.show();

    }

谢谢:)