我已经实现了“将墙纸设置为主屏幕”的实现。但是我没有得到如何将墙纸设置为锁屏和(家庭和锁)两者。在以下代码中,“如果”部分用于设置为主屏幕。 [“ C0]
将“墙纸”设置为“锁定”和“同时(家庭和锁定)”。代码:
ss of screen
}
检查我的代码是否对您有用:)
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();
}
});
downloadimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(imgUrl));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.
Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//commented by Darwin
//request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/my_file.JPEG");
//added by Darwin
FileUtils.createDirectory(AppConstants.DOWNLOADS_FOLDER);
File imageFile= new File(AppConstants.DOWNLOADS_FOLDER + "hair_" + System.currentTimeMillis() + ".jpg");
request.setDestinationUri(Uri.fromFile(imageFile));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File",
Toast.LENGTH_LONG).show();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Insert Subject here");
// String app_url = " https://play.google.com/store/apps/details?id=my.example.javatpoint";
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, imgUrl);
startActivity(Intent.createChooser(shareIntent, "Share via"));
}
});
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]))
{
Bitmap bitmap= StringToBitMap(imgUrl);
final Bitmap finalBitmap = bitmap;
try {
myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
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();
}
在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(); }
谢谢:)