Android設(shè)置默認(rèn)鎖屏壁紙接口的方法
本文實(shí)例為大家分享了Android設(shè)置默認(rèn)鎖屏壁紙接口的具體代碼,供大家參考,具體內(nèi)容如下
完成自定義service后,接下來(lái)就是具體實(shí)現(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中實(shí)現(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中對(duì)AIDL文件中定義的接口進(jìn)行具體實(shí)現(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中注冊(cè)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、因?yàn)槲覀冎皇窃贑ustomizedService 中調(diào)用setLockScreenWallpaper方法啟動(dòng)LockScreenWallPaperService,所以設(shè)置默認(rèn)wallpaper還是要由setLockScreenWallpaper實(shí)現(xiàn)的.下面要實(shí)現(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;
? ? }
?
}然后編譯一下,就可以通過(guò)接口設(shè)置默認(rèn)桌面了,大功告成。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android ImageView 的幾點(diǎn)經(jīng)驗(yàn)總結(jié)
本篇文章是對(duì)android中ImageView的使用技巧進(jìn)行了幾點(diǎn)經(jīng)驗(yàn)總結(jié),需要的朋友參考下2013-06-06
Flutter實(shí)現(xiàn)簡(jiǎn)單的內(nèi)容高亮效果
內(nèi)容高亮并不陌生,特別是在搜索內(nèi)容頁(yè)面,可以說(shuō)四處可見(jiàn),這篇文章主要為大家介紹了如何使用Flutter實(shí)現(xiàn)簡(jiǎn)單的內(nèi)容高亮效果,需要的可以參考下2023-08-08
Android實(shí)現(xiàn)畫(huà)板、寫(xiě)字板功能(附源碼下載)
這篇文章主要介紹了Android實(shí)現(xiàn)畫(huà)板、寫(xiě)字板功能的方法,文中給出了簡(jiǎn)單的介紹和示例代碼,想要了解更多的朋友可以下載源碼進(jìn)行學(xué)習(xí),感興趣的朋友們下面來(lái)一起看看吧。2017-01-01
Android Broadcast 和 BroadcastReceiver的權(quán)限限制方式
這篇文章主要介紹了Android Broadcast 和 BroadcastReceiver的權(quán)限限制方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
2014值得推薦的10個(gè)移動(dòng) Web 應(yīng)用程序開(kāi)發(fā)框架
今天這篇文章向大家推薦10大優(yōu)秀的移動(dòng) Web 開(kāi)發(fā)框架,幫助開(kāi)發(fā)者更加高效的開(kāi)發(fā)移動(dòng)Web應(yīng)用。2014-08-08
android BottomSheetDialog新控件解析實(shí)現(xiàn)知乎評(píng)論列表效果(實(shí)例代碼)
BottomSheetDialog是一個(gè)自定義的從底部滑入的對(duì)話框,這篇文章主要介紹了android BottomSheetDialog新控件解析實(shí)現(xiàn)知乎評(píng)論列表效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android利用RenderScript實(shí)現(xiàn)毛玻璃模糊效果示例
毛玻璃效果(亦稱磨砂效果),近兩年在移動(dòng)端的UI設(shè)計(jì)上越來(lái)越流行,下面這篇文章主要介紹了Android利用RenderScript實(shí)現(xiàn)毛玻璃模糊效果的相關(guān)資料,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考學(xué)習(xí),下面來(lái)一起看看吧。2017-03-03

