设置壁纸(静态壁纸)
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;
}
}