安卓A15系统实现修改锁屏界面默认壁纸功能

最近遇到一个A15系统项目,客户要求修改锁屏界面的默认壁纸,客户提供了一张壁纸图片,但是从A15系统的源代码查看时才知道谷歌已经去掉了相关的代码,已经不支持了,A13和A14系统好像是支持的,A15系统的WallpaperManager.java有明显不支持的提示如下:

 所以必须采用其他的方法来实现,具体修改如下:

 1.将客户给的锁屏壁纸图片拷贝到源代码中,然后添加一行拷贝代码将图片拷贝到system/media/目录下即可,如下:

2. 到packages/apps/Settings/src/com/android/settings/SettingsInitialize.java这个类文件中去修改代码,为什么选择在这个文件去修改呢?是因为这个SettingsInitialize.java文件是机器第一次开机进系统时会执行一次,以后的重启或者开机都不会执行了,就不用去添加系统属性标记位去判断是否是第一次开机进系统,具体修改如下:

 

diff --git a/src/com/android/settings/SettingsInitialize.java b/src/com/android/settings/SettingsInitialize.java
index 4887e26940c..0b04660c95b 100644
--- a/src/com/android/settings/SettingsInitialize.java
+++ b/src/com/android/settings/SettingsInitialize.java
@@ -46,6 +46,16 @@ import com.android.settingslib.utils.ThreadUtils;
 import java.util.ArrayList;
 import java.util.List;
 
+import android.app.WallpaperManager;
+import android.graphics.Bitmap;
+import java.io.ByteArrayOutputStream;
+import java.io.ByteArrayInputStream;
+import android.os.Handler;
+import android.content.Context;
+import android.graphics.BitmapFactory;
+import java.io.IOException;
+
+
 /**
  * Listens to {@link Intent.ACTION_PRE_BOOT_COMPLETED} and {@link Intent.ACTION_USER_INITIALIZE}
  * performs setup steps for a managed profile (disables the launcher icon of the Settings app,
@@ -58,6 +68,7 @@ public class SettingsInitialize extends BroadcastReceiver {
     private static final String PRIMARY_PROFILE_SETTING =
             "com.android.settings.PRIMARY_PROFILE_CONTROLLED";
     private static final String WEBVIEW_IMPLEMENTATION_ACTIVITY = ".WebViewImplementation";
+    private Context mContext;
 
     @Override
     public void onReceive(Context context, Intent broadcast) {
@@ -69,8 +80,41 @@ public class SettingsInitialize extends BroadcastReceiver {
         webviewSettingSetup(context, pm, userInfo);
         ThreadUtils.postOnBackgroundThread(() -> refreshExistingShortcuts(context));
         enableTwoPaneDeepLinkActivityIfNecessary(pm, context);
+
+        mContext = context;
+        mLockScreenHandler.removeCallbacks(mLockScreenRunnable);
+        mLockScreenHandler.postDelayed(mLockScreenRunnable,1);
     }
 
+    private Runnable mLockScreenRunnable = new Runnable() {
+        @Override
+        public void run() {
+           setLockScreenWallpaper();
+        }
+    };
+
+    private Handler mLockScreenHandler = new Handler();
+
+    private void setLockScreenWallpaper() {
+        WallpaperManager wallpaperManager = WallpaperManager.getInstance(mContext);
+        int FLAG_SET_LOCK = 1 << 1;
+        try {
+            //InputStream inputStream = new InputStream(new File("system/media/default_lock_wallpaper.png"));
+            //Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
+            Bitmap bitmap = BitmapFactory.decodeFile("system/media/default_lock_wallpaper.png");
+            //bitmap.eraseColor(0xFFBB86FC); //设置成紫色,有对应图片的话可以设置成对应图片
+            ByteArrayOutputStream tmpOut = new ByteArrayOutputStream(2048);
+            if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, tmpOut)) {
+                byte[] outByteArray = tmpOut.toByteArray();
+                wallpaperManager.setStream(new ByteArrayInputStream(outByteArray), null,
+                                true, FLAG_SET_LOCK);
+            }
+        } catch (IOException e) {
+
+		}
+		
+     }
+
     private void managedProfileSetup(Context context, final PackageManager pm, Intent broadcast,
             UserInfo userInfo) {
         if (userInfo == null || !userInfo.isManagedProfile()) {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值