IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> 代码设置桌面壁纸或者屏保 -> 正文阅读

[移动开发]代码设置桌面壁纸或者屏保

  • 今天公司让弄一个把图片和视频设置为屏保和壁纸的功能,刚开始在网上找了很多资料 设置壁纸很好实现 一句话就可以了,这个可以参考这个人写的代码 图片视频设置为壁纸 但是实现设置锁屏屏保就不大好实现了,后来想了想 如果能跳转到系统设置壁纸的界面 由用户自己选择设置壁纸 或者 屏保不就行了 于是百度,最后整理出来 有需要的拿走 直接上代码

  • MainActivity

package com.wallpaper.sample;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 设置壁纸
     */
    private TextView mAaadfa;
    /**
     * 设置图片壁纸
     */
    private TextView mPicWallPaper;
    /**
     * 设置视频壁纸
     */
    private TextView mVideoWallPaper;
    private WallPaperUtil paperUtil;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        paperUtil = new WallPaperUtil(this);
    }


    private void initView() {
        mAaadfa = (TextView) findViewById(R.id.picWallPaper);
        mAaadfa.setOnClickListener(this);
        mPicWallPaper = (TextView) findViewById(R.id.picWallPaper);
        mPicWallPaper.setOnClickListener(this);
        mVideoWallPaper = (TextView) findViewById(R.id.videoWallPaper);
        mVideoWallPaper.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.picWallPaper:
                File file = new File(Environment.getExternalStorageDirectory() + "/bbb.jpg");

                if (RomUtil.isEmui()) {
                    paperUtil.setHuawei(file.getAbsolutePath(), getUri(file));
                    return;
                }
                if (RomUtil.isMiui()) {
                    paperUtil.setXiaomi(file.getAbsolutePath(), getUri(file));
                    return;
                }
                if (RomUtil.isOppo()) {
                    paperUtil.setOppo(file.getAbsolutePath(), getUri(file));
                    return;
                }
                if (RomUtil.isVivo()) {
                    paperUtil.setVivo(file.getAbsolutePath(), getUri(file));
                    return;
                }
                paperUtil.setWallpaperDefault(file.getAbsolutePath());
                break;
            case R.id.videoWallPaper:
                startActivity(new Intent(this, VideoWallPaperActivity.class));
                break;
        }
    }


    /**
     * 获取对应intent
     *
     * @param file
     * @return
     */
    public Uri getUri(File file) {
        // Infer MIME type if missing for file URLs.
        Uri uri = null;
        if (Build.VERSION.SDK_INT >= 24) {//7.0 Android N
            //com.xxx.xxx.fileprovider为上述manifest中provider所配置相同
            uri = FileProvider.getUriForFile(this, "com.wallpaper.sample.fileProvider", file);
            // 读取权限,安装完毕以后,系统会自动收回权限,该过程没有用户交互
        } else {//7.0以下
            uri = Uri.fromFile(file);
        }

        return uri;
    }

}
  • WallPaperUtil
package com.wallpaper.sample;

import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import java.io.IOException;

/**
 * @Author: tobato
 * @Description: 作用描述
 * @CreateDate: 2021/7/31 16:38
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/7/31 16:38
 */
public class WallPaperUtil {

    Context context;

    public WallPaperUtil(Context context) {
        this.context = context.getApplicationContext();
    }

    /**
     * 华为
     */
    public void setHuawei( String path, Uri uriPath) {
        Intent intent;
        try {
            ComponentName componentName =
                    new ComponentName("com.android.gallery3d", "com.android.gallery3d.app.Wallpaper");
            intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(uriPath, "image/*");
            intent.putExtra("mimeType", "image/*");
            intent.setComponent(componentName);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            setWallpaperDefault(path);
        }
    }

    /**
     * 小米
     */
    public void setXiaomi( String path, Uri uriPath) {
        Intent intent;
        try {
            ComponentName componentName = new ComponentName("com.android.thememanager",
                    "com.android.thememanager.activity.WallpaperDetailActivity");
            intent = new Intent("miui.intent.action.START_WALLPAPER_DETAIL");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(uriPath, "image/*");
            intent.putExtra("mimeType", "image/*");
            intent.setComponent(componentName);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            setWallpaperDefault( path);
        }
    }

    /**
     * Vivo
     */
    public void setVivo(String path, Uri uriPath) {
        Intent intent;
        try {
            ComponentName componentName =
                    new ComponentName("com.vivo.gallery", "com.android.gallery3d.app.Wallpaper");
            intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(uriPath, "image/*");
            intent.putExtra("mimeType", "image/*");
            intent.setComponent(componentName);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            setWallpaperDefault( path);
        }
    }


    /**
     * OPPO
     */
    public void setOppo( String path, Uri uriPath) {
        Intent intent;
        try {
            ComponentName componentName =
                    new ComponentName("com.coloros.gallery3d", "com.oppo.gallery3d.app.Wallpaper");
            intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(uriPath, "image/*");
            intent.putExtra("mimeType", "image/*");
            intent.setComponent(componentName);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            setWallpaperDefault( path);
        }
    }


    /**
     * 默认方法
     */
    public void setWallpaperDefault( String path) {
        try {
            WallpaperManager.getInstance(context.getApplicationContext()).setBitmap(getBitmap(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取bitmap
     *
     * @param path
     */
    private Bitmap getBitmap(String path) {
        final Bitmap[] resource1 = {null};
        Glide.with(context).asBitmap().load(path).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                resource1[0] = resource;
            }
        });
        return resource1[0];
    }

}

  • RomUtil
package com.wallpaper.sample;

import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author: tobato
 * @Description: 作用描述  判断手机ROM,检测ROM是MIUI、EMUI还是Flyme
 * @CreateDate: 2020/5/25 9:46
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/5/25 9:46
 */
public class RomUtil {

        private static final String TAG = "Rom";

        public static final String ROM_MIUI = "MIUI";
        public static final String ROM_EMUI = "EMUI";
        public static final String ROM_FLYME = "FLYME";
        public static final String ROM_OPPO = "OPPO";
        public static final String ROM_SMARTISAN = "SMARTISAN";
        public static final String ROM_VIVO = "VIVO";
        public static final String ROM_QIKU = "QIKU";

        private static final String KEY_VERSION_MIUI = "ro.miui.ui.version.name";
        private static final String KEY_VERSION_EMUI = "ro.build.version.emui";
        private static final String KEY_VERSION_OPPO = "ro.build.version.opporom";
        private static final String KEY_VERSION_SMARTISAN = "ro.smartisan.version";
        private static final String KEY_VERSION_VIVO = "ro.vivo.os.version";

        private static String sName;
        private static String sVersion;

        //华为
        public static boolean isEmui() {
            return check(ROM_EMUI);
        }
        //小米
        public static boolean isMiui() {
            return check(ROM_MIUI);
        }
        //vivo
        public static boolean isVivo() {
            return check(ROM_VIVO);
        }
        //oppo
        public static boolean isOppo() {
            return check(ROM_OPPO);
        }
        //魅族
        public static boolean isFlyme() {
            return check(ROM_FLYME);
        }
        //360手机
        public static boolean is360() {
            return check(ROM_QIKU) || check("360");
        }

        public static boolean isSmartisan() {
            return check(ROM_SMARTISAN);
        }

        public static String getName() {
            if (sName == null) {
                check("");
            }
            return sName;
        }

        public static String getVersion() {
            if (sVersion == null) {
                check("");
            }
            return sVersion;
        }

        private static boolean check(String rom) {
            if (sName != null) {
                return sName.equals(rom);
            }

            if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_MIUI))) {
                sName = ROM_MIUI;
            } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_EMUI))) {
                sName = ROM_EMUI;
            } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_OPPO))) {
                sName = ROM_OPPO;
            } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_VIVO))) {
                sName = ROM_VIVO;
            } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_SMARTISAN))) {
                sName = ROM_SMARTISAN;
            } else {
                sVersion = Build.DISPLAY;
                if (sVersion.toUpperCase().contains(ROM_FLYME)) {
                    sName = ROM_FLYME;
                } else {
                    sVersion = Build.UNKNOWN;
                    sName = Build.MANUFACTURER.toUpperCase();
                }
            }
            return sName.equals(rom);
        }

    private static String getProp(String name) {
            String line = null;
            BufferedReader input = null;
            try {
                Process p = Runtime.getRuntime().exec("getprop " + name);
                input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
                line = input.readLine();
                input.close();
            } catch (IOException ex) {
                Log.e(TAG, "Unable to read prop " + name, ex);
                return null;
            } finally {
                if (input != null) {
                    try {
                        input.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return line;
        }
    }

  • 注意 记得加权限
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • 7.0文件路径的问题记得适配 还有就是本地根目录下得有bbb.jpg这个图片文件
  • 希望能帮到你!!
  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-08-01 14:37:31  更:2021-08-01 14:38:39 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/7 6:20:43-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码