Android 设置默认锁屏壁纸接口

news/2024/11/28 3:31:07/

完成自定义service后,接下来就是具体实现接口

1,在frameworks/base/core/java/android/app/customized/ICustomizedService.aidl中定义接口

boolean setLockScreenWallpaper(String uri);

2,在frameworks/base/core/java/android/app/customized/CustomizedManager.java中实现接口

package android.app.customized;import android.util.Log;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.RemoteException;
import android.provider.Settings;
import java.io.IOException;
import android.os.ServiceManager;
import android.os.IBinder;
import java.util.List;
import android.app.ActivityManager;
import android.graphics.Bitmap;public class CustomizedManager{private static final String TAG="CustomizedManager";private static final boolean DBG=true;private static ICustomizedService mService;private final Context mContext;public CustomizedManager(Context context){mContext = context;mService = ICustomizedService.Stub.asInterface(ServiceManager.getService("customized"));}private static ICustomizedService getService(){if (mService != null) {return mService;}IBinder b = ServiceManager.getService("customized"mService = ICustomizedService.Stub.asInterface(b);return mService;}public boolean setLockScreenWallpaper(String uri) {try {getService().setLockScreenWallpaper(uri);} catch (RemoteException e) {}return false;}}

3,在frameworks/base/services/core/java/com/android/server/customized/CustomizedService.java中对AIDL文件中定义的接口进行具体实现.

package com.android.server.customized;import android.os.IBinder;
import android.os.ServiceManager;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.app.customized.ICustomizedService;
import android.content.BroadcastReceiver;
import android.view.IWindowManager;
import android.view.WindowManagerGlobal;
import android.graphics.BitmapFactory;public class CustomizedService extends ICustomizedService.Stub {private static final String TAG = "CustomizedService ";private Context mContext;public static class Lifecycle extends SystemService {private CustomizedService mService;public Lifecycle(Context context) {super(context);}@Overridepublic void onStart() {mService = new CustomizedService (getContext());publishBinderService(Context.CUSTOMIZED, mService);}@Overridepublic void onBootPhase(int phase) {}@Overridepublic void onUnlockUser(int userHandle) {}}public CustomizedService (Context context) {mContext = context;}public boolean setLockScreenWallpaper(String uri) {if (uri == null || "".equals(uri))return false;File file = new File(uri);if (!file.exists()) {return false;}Log.d(TAG, "setLockScreenWallpaper uri===============" + uri);long ident = Binder.clearCallingIdentity();Intent sendlock = new Intent();String packageName = "com.android.launcher3";String serviceClassName = packageName + ".LockScreenWallPaperService";sendlock.putExtra("path", uri);sendlock.setComponent(new ComponentName(packageName, serviceClassName));mContext.startServiceAsUser(sendlock, UserHandle.OWNER);Binder.restoreCallingIdentity(ident);return true;}}

4, 在packages/apps/Launcher3/AndroidManifest.xml中注册LockScreenWallPaperService

        <serviceandroid:name="com.android.launcher3.LockScreenWallPaperService"android:exported="true" ><intent-filter><action android:name="com.android.launcher.action.SET_LOCKSCREENWALLPAPER_SERVICE" /></intent-filter></service>

5,因为我们只是在CustomizedService 中调用setLockScreenWallpaper方法启动LockScreenWallPaperService,所以设置默认wallpaper还是要由setLockScreenWallpaper实现的.下面要实现LockScreenWallPaperService了,路径为

packages/apps/Launcher3/src/com/android/launcher3/LockScreenWallPaperService.java 

package com.android.launcher3;import android.app.Service;
import android.os.*;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.content.Context;
import android.content.Intent;
import android.graphics.Matrix;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;public class LockScreenWallPaperService extends Service {private String TAG = "LockScreenWallPaperService";private String path = "";@Overridepublic void onCreate() {}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(TAG, "onStartCommand ");if (intent != null) {path = intent.getStringExtra("path");}Bitmap bitmap = BitmapFactory.decodeFile(path);SavePicToLocal savePic = new SavePicToLocal(bitmap);savePic.execute("save picture");return START_STICKY;}public boolean dumpBitmap(Bitmap mBitmap) throws FileNotFoundException {Log.d(TAG, "dumpBitmap");boolean flagSaveCompelete = false;Bitmap bitmap_land, bitmap_port;int height = mBitmap.getHeight();int width = mBitmap.getWidth();int lswidth = 1920;int lsheight = 1200;float lper = Math.max((float) lswidth / (float) width, (float) lsheight/ (float) height);if (lper > 1) {Matrix lmatrix = new Matrix();lmatrix.postScale(lper, lper);bitmap_land = Bitmap.createBitmap(mBitmap,(int) ((mBitmap.getWidth() - lswidth / lper) / 2),(int) ((mBitmap.getHeight() - lsheight / lper) / 2),(int) (lswidth / lper), (int) (lsheight / lper), lmatrix,true);} else {bitmap_land = Bitmap.createBitmap(mBitmap,(int) ((mBitmap.getWidth() - lswidth) / 2),(int) ((mBitmap.getHeight() - lsheight) / 2), lswidth,lsheight, null, true);}int pswidth = 1200;int psheight = 1920;float pper = Math.max((float) pswidth / (float) width, (float) psheight/ (float) height);if (pper > 1) {Matrix pmatrix = new Matrix();pmatrix.postScale(pper, pper);bitmap_port = Bitmap.createBitmap(mBitmap,(int) ((mBitmap.getWidth() - pswidth / pper) / 2),(int) ((mBitmap.getHeight() - psheight / pper) / 2),(int) (pswidth / pper), (int) (psheight / pper), pmatrix,true);} else {bitmap_port = Bitmap.createBitmap(mBitmap,(int) ((mBitmap.getWidth() - pswidth) / 2),(int) ((mBitmap.getHeight() - psheight) / 2), pswidth,psheight, null, true);}Matrix matrix = new Matrix();matrix.postScale(0.5f, 0.5f);bitmap_land = Bitmap.createBitmap(bitmap_land, 0, 0,bitmap_land.getWidth(), bitmap_land.getHeight(), matrix, true);bitmap_port = Bitmap.createBitmap(bitmap_port, 0, 0,bitmap_port.getWidth(), bitmap_port.getHeight(), matrix, true);flagSaveCompelete = saveBitmapToFile(bitmap_port,"/data/local/tmp/lockscreenwallpaper/keyguard_wallpaper_land.png",1);flagSaveCompelete = saveBitmapToFile(bitmap_land,"/data/local/tmp/lockscreenwallpaper/keyguard_wallpaper_port.png",2);return flagSaveCompelete;}private boolean saveBitmapToFile(Bitmap bitmap, String path, int isRecycle)throws FileNotFoundException {Log.d(TAG, "saveBitmapToFile ident=" + "bitmap" + bitmap);boolean result = false;if (bitmap == null)return result;Bitmap tmpbm = null;java.io.FileOutputStream tmpfos = null;try {tmpbm = bitmap;tmpfos = new java.io.FileOutputStream(path);tmpbm.compress(Bitmap.CompressFormat.PNG, 100, tmpfos);Log.d(TAG, "saveBitmapToFile compress");} catch (FileNotFoundException ex) {Log.d(TAG, "ex1" + ex);throw ex;} catch (java.io.IOException ex) {Log.d(TAG, "ex2" + ex);ex.printStackTrace();} finally {if (tmpfos != null) {try {Log.d(TAG, "tmpfos.close() ");tmpfos.close();result = true;} catch (java.io.IOException ex) {Log.d(TAG, "ex3" + ex);}}if (tmpbm != null && !tmpbm.isRecycled())if (isRecycle == 2) {tmpbm.recycle();}}return result;}class SavePicToLocal extends AsyncTask<String, Integer, Boolean> {Bitmap bitmap;public SavePicToLocal(Bitmap mBitmap) {bitmap = mBitmap;}@Overrideprotected Boolean doInBackground(String... params) {return dumpBitmaps();}private boolean dumpBitmaps() {boolean flag = false;try {flag = dumpBitmap(bitmap);flag = true;} catch (FileNotFoundException e) {}return flag;}@Overrideprotected void onPostExecute(Boolean result) {if (result) {sendBroadcast(new Intent("android.intent.action.UPDATE_LOCK_WALLPAPER"));Log.d(TAG, "send UPDATE_LOCK_WALLPAPER");}}@Overrideprotected void onProgressUpdate(Integer... values) {super.onProgressUpdate(values);}}@Overridepublic IBinder onBind(Intent intent) {return null;}}

然后编译一下,就可以通过接口设置默认桌面了,大功告成~


http://www.ppmy.cn/news/446765.html

相关文章

设置电脑锁屏壁纸

1、快捷键Winr输入regedit&#xff0c;依次打开HKEY_LOCAL_MACHINE、SOFTWARE、Microsoft、Windows、CurrentVersion、Authentication、LogonUI、Background将 数值设置为1表示自定义的图片&#xff08;网上说不能大于257k&#xff09; 2、C:\Windows\System32\oobe\info\back…

修改设置Android锁屏壁纸为系统默认的锁屏壁纸

最近做了项目上的需求&#xff0c;就是在设备锁屏之后&#xff0c;要恢复一下系统默认的壁纸。。。 设置锁屏壁纸的方法 很明显如果想要修改系统锁屏壁纸&#xff0c;需要用到这个类WallpaperManager&#xff0c;按照正常的方法调用有类似下面这样的方法可以使用&#xff1a;…

android 设置锁屏壁纸

客户功能需求&#xff1a;设置可以自定义的锁屏壁纸。 分析&#xff1a;首先分析系统壁纸设置过程&#xff0c;参考 上一期文章&#xff0c;Android5.1 设置壁纸流程简析。可以按照设置壁纸的方式&#xff0c;模仿实现设置锁屏壁纸的功能。 利用广播&#xff0c;实现设置锁屏…

自定义注解接收消息头信息

自定义参数解析 ① 自定义注解②自定义参数解析器③实现Springboot的自动装配接口④将自定义注解添加到参数上 前提&#xff1a; springboot项目是必须的&#xff0c;因为需要用到自动装配 ① 自定义注解 Target(ElementType.PARAMETER) // 代表 加在 方法的参数上 (Token…

SQL基础入门-条件查询语句

前言 可以关注我的云原生社区&#xff1a;云原生社区 也可以关注我的英语社区&#xff1a;从零开始学英语 一. 创建数据库并写入数据 1.1 创建数据库 MySQL [school]> create database game; Query OK, 1 row affected (0.01 sec)MySQL [school]> use game Database c…

DJ4-1 网络层概述

目录 一、网络层提供的功能 二、路由和转发 三、数据平面和控制平面 四、网络层的服务模型 一、网络层提供的功能 网络层实现主机与主机之间的通信 从发送方主机传输报文段到接收方主机&#xff1a; 发送方主机封装报文段 (segments) 为数据报 (datagrams)接收方主机递交…

使用Apache ShardingSphere简答实现水平分表

1 简介 Apache ShardingSphere 是一款分布式的数据库生态系统&#xff0c; 可以将任意数据库转换为分布式数据库&#xff0c;并通过数据分片、弹性伸缩、加密等能力对原有数据库进行增强。 官方网站 https://shardingsphere.apache.org/document/current/cn/overview/2 创建…

黑莓手机企业激活注意事项和可能问题

手机能否打电话 手机网络模式 电信UIM卡&#xff1a;手机要设置为Global或者EVDO 移动SIM卡&#xff1a;手机j建议设置为GSM/2G 联通UIM卡&#xff1a;手机j建议设置为3G 手机能否上网 浏览器访问www.baidu.com 提示&#xff1a;某些黑莓手机做过企业激活了&#xff0c;前一个公…