Android如何设置默认锁屏壁纸接口

Android如何设置默认锁屏壁纸接口

本篇内容主要讲解“Android如何设置默认锁屏壁纸接口”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android如何设置默认锁屏壁纸接口”吧!

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

Android如何设置默认锁屏壁纸接口

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

booleansetLockScreenWallpaper(Stringuri);

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

packageandroid.app.customized;importandroid.util.Log;importandroid.content.Context;importandroid.content.Intent;importandroid.os.Binder;importandroid.os.RemoteException;importandroid.provider.Settings;importjava.io.IOException;importandroid.os.ServiceManager;importandroid.os.IBinder;importjava.util.List;importandroid.app.ActivityManager;importandroid.graphics.Bitmap;publicclassCustomizedManager{privatestaticfinalStringTAG="CustomizedManager";privatestaticfinalbooleanDBG=true;privatestaticICustomizedServicemService;privatefinalContextmContext;publicCustomizedManager(Contextcontext){mContext=context;mService=ICustomizedService.Stub.asInterface(ServiceManager.getService("customized"));}privatestaticICustomizedServicegetService(){if(mService!=null){returnmService;}IBinderb=ServiceManager.getService("customized"mService=ICustomizedService.Stub.asInterface(b);returnmService;}publicbooleansetLockScreenWallpaper(Stringuri){try{getService().setLockScreenWallpaper(uri);}catch(RemoteExceptione){}returnfalse;}}

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

packagecom.android.server.customized;importandroid.os.IBinder;importandroid.os.ServiceManager;importandroid.content.Context;importandroid.content.Intent;importandroid.os.Binder;importandroid.app.customized.ICustomizedService;importandroid.content.BroadcastReceiver;importandroid.view.IWindowManager;importandroid.view.WindowManagerGlobal;importandroid.graphics.BitmapFactory;publicclassCustomizedServiceextendsICustomizedService.Stub{privatestaticfinalStringTAG="CustomizedService";privateContextmContext;publicstaticclassLifecycleextendsSystemService{privateCustomizedServicemService;publicLifecycle(Contextcontext){super(context);}@OverridepublicvoidonStart(){mService=newCustomizedService(getContext());publishBinderService(Context.CUSTOMIZED,mService);}@OverridepublicvoidonBootPhase(intphase){}@OverridepublicvoidonUnlockUser(intuserHandle){}}publicCustomizedService(Contextcontext){mContext=context;}publicbooleansetLockScreenWallpaper(Stringuri){if(uri==null||"".equals(uri))returnfalse;Filefile=newFile(uri);if(!file.exists()){returnfalse;}Log.d(TAG,"setLockScreenWallpaperuri==============="+uri);longident=Binder.clearCallingIdentity();Intentsendlock=newIntent();StringpackageName="com.android.launcher3";StringserviceClassName=packageName+".LockScreenWallPaperService";sendlock.putExtra("path",uri);sendlock.setComponent(newComponentName(packageName,serviceClassName));mContext.startServiceAsUser(sendlock,UserHandle.OWNER);Binder.restoreCallingIdentity(ident);returntrue;}}

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

<serviceandroid:name="com.android.launcher3.LockScreenWallPaperService"android:exported="true"><intent-filter><actionandroid: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

packagecom.android.launcher3;importandroid.app.Service;importandroid.os.*;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.content.Context;importandroid.content.Intent;importandroid.graphics.Matrix;importandroid.util.Log;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;publicclassLockScreenWallPaperServiceextendsService{privateStringTAG="LockScreenWallPaperService";privateStringpath="";@OverridepublicvoidonCreate(){}@OverridepublicintonStartCommand(Intentintent,intflags,intstartId){Log.d(TAG,"onStartCommand");if(intent!=null){path=intent.getStringExtra("path");}Bitmapbitmap=BitmapFactory.decodeFile(path);SavePicToLocalsavePic=newSavePicToLocal(bitmap);savePic.execute("savepicture");returnSTART_STICKY;}publicbooleandumpBitmap(BitmapmBitmap)throwsFileNotFoundException{Log.d(TAG,"dumpBitmap");booleanflagSaveCompelete=false;Bitmapbitmap_land,bitmap_port;intheight=mBitmap.getHeight();intwidth=mBitmap.getWidth();intlswidth=1920;intlsheight=1200;floatlper=Math.max((float)lswidth/(float)width,(float)lsheight/(float)height);if(lper>1){Matrixlmatrix=newMatrix();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);}intpswidth=1200;intpsheight=1920;floatpper=Math.max((float)pswidth/(float)width,(float)psheight/(float)height);if(pper>1){Matrixpmatrix=newMatrix();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);}Matrixmatrix=newMatrix();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);returnflagSaveCompelete;}privatebooleansaveBitmapToFile(Bitmapbitmap,Stringpath,intisRecycle)throwsFileNotFoundException{Log.d(TAG,"saveBitmapToFileident="+"bitmap"+bitmap);booleanresult=false;if(bitmap==null)returnresult;Bitmaptmpbm=null;java.io.FileOutputStreamtmpfos=null;try{tmpbm=bitmap;tmpfos=newjava.io.FileOutputStream(path);tmpbm.compress(Bitmap.CompressFormat.PNG,100,tmpfos);Log.d(TAG,"saveBitmapToFilecompress");}catch(FileNotFoundExceptionex){Log.d(TAG,"ex1"+ex);throwex;}catch(java.io.IOExceptionex){Log.d(TAG,"ex2"+ex);ex.printStackTrace();}finally{if(tmpfos!=null){try{Log.d(TAG,"tmpfos.close()");tmpfos.close();result=true;}catch(java.io.IOExceptionex){Log.d(TAG,"ex3"+ex);}}if(tmpbm!=null&&!tmpbm.isRecycled())if(isRecycle==2){tmpbm.recycle();}}returnresult;}classSavePicToLocalextendsAsyncTask<String,Integer,Boolean>{Bitmapbitmap;publicSavePicToLocal(BitmapmBitmap){bitmap=mBitmap;}@OverrideprotectedBooleandoInBackground(String...params){returndumpBitmaps();}privatebooleandumpBitmaps(){booleanflag=false;try{flag=dumpBitmap(bitmap);flag=true;}catch(FileNotFoundExceptione){}returnflag;}@OverrideprotectedvoidonPostExecute(Booleanresult){if(result){sendBroadcast(newIntent("android.intent.action.UPDATE_LOCK_WALLPAPER"));Log.d(TAG,"sendUPDATE_LOCK_WALLPAPER");}}@OverrideprotectedvoidonProgressUpdate(Integer...values){super.onProgressUpdate(values);}}@OverridepublicIBinderonBind(Intentintent){returnnull;}}

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

到此,相信大家对“Android如何设置默认锁屏壁纸接口”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

发布于 2022-01-06 23:31:22
收藏
分享
海报
0 条评论
38
上一篇:.NET Core怎么实现Cookie和JWT混合认证、授权 下一篇:常用16个c/c++面试题是什么
目录

    推荐阅读

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码