Android設(shè)置默認鎖屏壁紙接口的方法
本文實例為大家分享了Android設(shè)置默認鎖屏壁紙接口的具體代碼,供大家參考,具體內(nèi)容如下
完成自定義service后,接下來就是具體實現(xiàn)接口
1、在frameworks/base/core/java/android/app/customized/ICustomizedService.aidl中定義接口
boolean setLockScreenWallpaper(String uri);
2、在frameworks/base/core/java/android/app/customized/CustomizedManager.java中實現(xiàn)接口
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文件中定義的接口進行具體實現(xiàn).
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); ? ? ? ? } ? ? ? ? ? @Override ? ? ? ? public void onStart() { ? ? ? ? ? ? mService = new CustomizedService (getContext()); ? ? ? ? ? ? publishBinderService(Context.CUSTOMIZED, mService); ? ? ? ? } ? ? ? ? ?@Override ? ? ? ? public void onBootPhase(int phase) { ? ? ? ? } ? ? ? ? ? @Override ? ? ? ? public 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
<service ? ? ? ? ? ? android: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 中調(diào)用setLockScreenWallpaper方法啟動LockScreenWallPaperService,所以設(shè)置默認wallpaper還是要由setLockScreenWallpaper實現(xiàn)的.下面要實現(xiàn)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 = ""; ? ? ? @Override ? ? public void onCreate() { ? ? ? } ? ? ? @Override ? ? public 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; ? ? ? ? } ? ? ? ? ? @Override ? ? ? ? protected Boolean doInBackground(String... params) { ? ? ? ? ? ? return dumpBitmaps(); ? ? ? ? } ? ? ? ? ? private boolean dumpBitmaps() { ? ? ? ? ? ? boolean flag = false; ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? flag = dumpBitmap(bitmap); ? ? ? ? ? ? ? ? flag = true; ? ? ? ? ? ? } catch (FileNotFoundException e) { ? ? ? ? ? ? } ? ? ? ? ? ? return flag; ? ? ? ? } ? ? ? ? @Override ? ? ? ? protected void onPostExecute(Boolean result) { ? ? ? ? ? ? if (result) { ? ? ? ? ? ? ? ? sendBroadcast(new Intent( ? ? ? ? ? ? ? ? ? ? ? ? "android.intent.action.UPDATE_LOCK_WALLPAPER")); ? ? ? ? ? ? ? ? Log.d(TAG, "send UPDATE_LOCK_WALLPAPER"); ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? ? ? @Override ? ? ? ? protected void onProgressUpdate(Integer... values) { ? ? ? ? ? ? super.onProgressUpdate(values); ? ? ? ? } ? ? } ? ? ? @Override ? ? public IBinder onBind(Intent intent) { ? ? ? ? return null; ? ? } ? }
然后編譯一下,就可以通過接口設(shè)置默認桌面了,大功告成。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android 實現(xiàn)APP中改變頭像圖片的實例代碼
這篇文章主要介紹了android 實現(xiàn)APP中改變頭像圖片的實例代碼,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07Android三種方式生成矢量圖之VectorDrawable類使用詳解
這篇文章主要介紹了Android三種方式生成矢量圖的VectorDrawable類,2014年6月26日的I/O?2014開發(fā)者大會上谷歌正式推出了Android?L,它帶來了全新的設(shè)計語言Material?Design,新的API也提供了這個類VectorDrawable2023-02-02Android開發(fā)之App widget用法實例分析
這篇文章主要介紹了Android開發(fā)之App widget用法,結(jié)合實例形式詳細分析了Android開發(fā)中使用App widget組件的具體步驟與相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06