设置壁纸(静态壁纸)

设置壁纸(静态壁纸)

1.权限
<uses-permission android:name="android.permission.SET_WALLPAPER" />
2.系统接口
//获取壁纸管理者
val wallpaperManager = WallpaperManager.getInstance(BaseApplication.context)
val inputStream = BaseApplication.context.contentResolver.openInputStream(uri)
//android 7.0之后可以设置锁屏壁纸
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    //参数    图片资源流   图片显示区域  是否允许备份  壁纸标志:WallpaperManager.FLAG_LOCK(锁屏)、WallpaperManager.FLAG_SYSTEM(桌面)
    wallpaperManager.setStream(inputStream,null,true,WallpaperManager.FLAG_SYSTEM)
}else{
    wallpaperManager.setStream(inputStream)
}
3.针对7.0以下锁屏壁纸无法设置兼容
public static void setWallpaper(Context context, String uri, String authority) {
    if (context == null || TextUtils.isEmpty(uri) || TextUtils.isEmpty(authority)) {
        return;
    }
    Intent intent;
    if (RomUtil.isHuaweiRom()) {
        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.setDataAndType(Uri.parse(uri), "image/*");
            intent.putExtra("mimeType", "image/*");
            intent.setComponent(componentName);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse(uri));
                WallpaperManager.getInstance(context.getApplicationContext())
                .setStream(inputStream);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    } else if (RomUtil.isMiuiRom()) {
        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.setDataAndType(Uri.parse(uri), "image/*");
            intent.putExtra("mimeType", "image/*");
            intent.setComponent(componentName);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse(uri));
                WallpaperManager.getInstance(context.getApplicationContext())
                .setStream(inputStream);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                intent =
                WallpaperManager.getInstance(context.getApplicationContext()).getCropAndSetWallpaperIntent(Uri.parse(uri));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.getApplicationContext().startActivity(intent);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                try {
                    InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse(uri));
                    WallpaperManager.getInstance(context.getApplicationContext())
                    .setStream(inputStream);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        } else {
            try {
                InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse(uri));
                WallpaperManager.getInstance(context.getApplicationContext())
                .setStream(inputStream);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

RomUtil

public class RomUtil {

    private static final String TAG = "RomUtil";

    /**
     * 判断是否为华为系统
     */
    public static boolean isHuaweiRom() {
        if (!TextUtils.isEmpty(getEmuiVersion()) && !getEmuiVersion().equals("")) {
            Log.d(TAG, "isHuaweiRom: true");
            return true;
        }
        Log.d(TAG, "isHuaweiRom: false");
        return false;
    }

    /**
     * 判断是否为小米系统
     */
    public static boolean isMiuiRom() {
        if (!TextUtils.isEmpty(getSystemProperty("ro.miui.ui.version.name"))) {
            Log.d(TAG, "isMiuiRom: true");
            return true;
        }
        Log.d(TAG, "isMiuiRom: false");
        return false;
    }

    private static String getSystemProperty(String propName) {
        String line;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + propName);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException ex) {
            Log.e(TAG, "Unable to read sysprop " + propName, ex);
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    Log.e(TAG, "Exception while closing InputStream", e);
                }
            }
        }
        return line;
    }

    /**
     * 判断是否为Flyme系统
     */
    public static boolean isFlymeRom(Context context) {
        Object obj = isInstalledByPkgName(context, "com.meizu.flyme.update") ? Log.d(TAG, "isFlymeRom: true")
            : Log.d(TAG, "isFlymeRom: false");
        return isInstalledByPkgName(context, "com.meizu.flyme.update");
    }

    /**
     * 判断是否是Smartisan系统
     */
    public static boolean isSmartisanRom(Context context) {
        Object obj = isInstalledByPkgName(context, "com.smartisanos.security") ? Log.d(TAG, "isSmartisanRom: true")
            : Log.d(TAG, "isSmartisanRom: false");
        return isInstalledByPkgName(context, "com.smartisanos.security");
    }

    /**
     * 根据包名判断这个app是否已安装
     */
    public static boolean isInstalledByPkgName(Context context, String pkgName) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(pkgName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
        }
        if (packageInfo == null) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * @return 只要返回不是"",则是EMUI版本
     */
    private static String getEmuiVersion() {
        String emuiVerion = "";
        Class<?>[] clsArray = new Class<?>[] { String.class };
        Object[] objArray = new Object[] { "ro.build.version.emui" };
        try {
            Class<?> SystemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method get = SystemPropertiesClass.getDeclaredMethod("get", clsArray);
            String version = (String) get.invoke(SystemPropertiesClass, objArray);
            Log.d(TAG, "get EMUI version is:" + version);
            if (!TextUtils.isEmpty(version)) {
                return version;
            }
        } catch (ClassNotFoundException e) {
            Log.e(TAG, " getEmuiVersion wrong, ClassNotFoundException");
        } catch (LinkageError e) {
            Log.e(TAG, " getEmuiVersion wrong, LinkageError");
        } catch (NoSuchMethodException e) {
            Log.e(TAG, " getEmuiVersion wrong, NoSuchMethodException");
        } catch (NullPointerException e) {
            Log.e(TAG, " getEmuiVersion wrong, NullPointerException");
        } catch (Exception e) {
            Log.e(TAG, " getEmuiVersion wrong");
        }
        return emuiVerion;
    }
}
Android设置桌面壁纸及恢复默认墙纸,程序中将创建一个存储壁纸图片资源的id数组,定义被选中的图片在id数组中的索引,需要自定义一个BaseAdapter,然后:   ImageView iv = new ImageView(Sample_12_2.this);//新建一个ImageView   iv.setBackgroundResource(imgIds[position]);//设置ImageView的背景图片   iv.setScaleType(ImageView.ScaleType.CENTER_CROP);   iv.setLayoutParams(new Gallery.LayoutParams(120, 120));//设置相框中元素的大小   将设置壁纸和恢复壁纸的功能写入按钮监听事件中,通过单击按钮来激活这两个功能:   设置ImageView为当前墙纸:   Button btnGetWall = (Button)findViewById(R.id.getWall);//获得Button对象   btnGetWall.setOnClickListener(new View.OnClickListener() {//为Button添加OnClickListener监听器   @Override   public void onClick(View v) {    ImageView iv = (ImageView)findViewById(R.id.currWall);    iv.setBackgroundDrawable(getWallpaper()); //设置ImageView显示的内容为当前墙纸   }   恢复默认的壁纸:   setContentView(R.layout.main);//设置当前屏幕   Button btnClearWall = (Button)findViewById(R.id.clearWall);//获得Button对象   btnClearWall.setOnClickListener(new View.OnClickListener() {//添加OnClickListener监听器   @Override   public void onClick(View v) {//重写onClick方法   try {    Sample_12_2.this.clearWallpaper();//还原手机壁纸   } catch (IOException e) {//捕获并打印异常    e.printStackTrace();   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值