Android如何设置默认锁屏壁纸接口
Android如何设置默认锁屏壁纸接口
本篇内容主要讲解“Android如何设置默认锁屏壁纸接口”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android如何设置默认锁屏壁纸接口”吧!
完成自定义service后,接下来就是具体实现接口
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如何设置默认锁屏壁纸接口”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
推荐阅读
-
android(如何快速开发框架 小米note开发版MIUI,安卓6.0,怎么安装Xposed框架)
稳定版,你必须先根除。你上网搜索安卓可以叫别人s框架,对方可以把框架做成jar包,把这个jar包加载到项目目录的libs文件中使...
-
android(studio 虚拟机启动不了 android studio可以当模拟器用吗)
androidstudio可以当模拟器用吗?AmdCUP引导模拟器有点复杂。雷电模拟器上的抖音怎么登录不上?不是,闪电模拟调用...
-
从实践中学习手机抓包与数据分析(android 手机抓包app)
android手机抓包app?netcapture抓包精灵app(手机抓包工具)又名sslcapture,是什么专业的安卓手机抓...
-
android(studio全局搜索 android studio怎么看app界面)
androidstudio怎么看app界面?在设备桌面点击运用直接进入到App界面,就也可以参与其他你的操作了。android-...
-
怎么把android框架源代码拉到本地(android studio如何运行别人的源代码)
androidstudio如何运行别人的源代码?androidstudio点击刚建在列表中你选择导入module,导入即可在用...
-
android(studio2022年使用教程 怎么安装Android studio详细教程)
怎么安装Androidstudio详细教程?androidstudio中haxm直接安装的方法追加:1、简单的方法打开Andr...
-
怎么使用Android基准配置文件Baseline Profile方案提升启动速度
-
HTML5如何实现禁止android视频另存为
-
学java好还是学php好?
-
Android如何实现多点触控功能