完成自定义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;}}
然后编译一下,就可以通过接口设置默认桌面了,大功告成~