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;
}
}
public Uri getUri(File file) {
Uri uri = null;
if (Build.VERSION.SDK_INT >= 24) {
uri = FileProvider.getUriForFile(this, "com.wallpaper.sample.fileProvider", file);
} else {
uri = Uri.fromFile(file);
}
return uri;
}
}
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;
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);
}
}
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);
}
}
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();
}
}
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];
}
}
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;
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);
}
public static boolean isVivo() {
return check(ROM_VIVO);
}
public static boolean isOppo() {
return check(ROM_OPPO);
}
public static boolean isFlyme() {
return check(ROM_FLYME);
}
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这个图片文件
- 希望能帮到你!!
|