如何将壁纸设置为锁屏或两者(家庭和锁定)?

Posted

技术标签:

【中文标题】如何将壁纸设置为锁屏或两者(家庭和锁定)?【英文标题】:how to set wallpaper as lockscreen or both(home and lock)? 【发布时间】:2020-03-25 15:09:30 【问题描述】:

我已经完成了“将壁纸设置为主屏幕”的实现。但我不知道如何将壁纸设置为锁屏和两者(主页和锁)。在下面的代码中“如果”部分用于设置为主屏幕..rest用于“锁定”和“两者(家庭和锁定)”..将壁纸设置为锁定和两者(家庭和锁定)的逻辑是什么?

代码:

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】:

检查我的代码是否适合你:)

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

在 OnCreate() 中

 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);

            
        );

在 OnActivityResult() 中

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();

    

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

WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM : for both Screen
WallpaperManager.FLAG_SYSTEM : for Home Screen
WallpaperManager.FLAG_LOCK :for Lock Screen

谢谢:)

【讨论】:

评论不用于扩展讨论;这个对话是moved to chat。

以上是关于如何将壁纸设置为锁屏或两者(家庭和锁定)?的主要内容,如果未能解决你的问题,请参考以下文章

仅设置 Android 主屏幕壁纸

UWP 从后台任务设置锁屏/壁纸

安卓手机如何自定义设置动态壁纸

iOS锁屏超实用功能化!设置教程

分享一波会眨眼的壁纸

如何设置win10锁屏壁纸