欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android设置默认锁屏壁纸接口的方法

程序员文章站 2022-06-01 20:25:40
...

本文实例为大家分享了Android设置默认锁屏壁纸接口的具体代码,供大家参考,具体内容如下

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

1、在frameworks/base/core/java/android/app/customized/ICustomizedService.aidl中定义接口
1 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;

  1. private static ICustomizedService mService;
  2. private final Context mContext;
  3. public CustomizedManager(Context context){
  4. mContext = context;
  5. mService = ICustomizedService.Stub.asInterface(
  6. ServiceManager.getService("customized"));
  7. }
  8. private static ICustomizedService getService(){
  9. if (mService != null) {
  10. return mService;
  11. }
  12. IBinder b = ServiceManager.getService("customized"
  13. mService = ICustomizedService.Stub.asInterface(b);
  14. return mService;
  15. }

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;

  1. public static class Lifecycle extends SystemService {
  2. private CustomizedService mService;
  3. public Lifecycle(Context context) {
  4. super(context);
  5. }
  6. @Override
  7. public void onStart() {
  8. mService = new CustomizedService (getContext());
  9. publishBinderService(Context.CUSTOMIZED, mService);
  10. }
  11. @Override
  12. public void onBootPhase(int phase) {
  13. }
  14. @Override
  15. public void onUnlockUser(int userHandle) {
  16. }
  17. }
  18. public CustomizedService (Context context) {
  19. mContext = context;

}

  1. public boolean setLockScreenWallpaper(String uri) {
  2. if (uri == null || "".equals(uri))
  3. return false;
  4. File file = new File(uri);
  5. if (!file.exists()) {
  6. return false;
  7. }
  8. Log.d(TAG, "setLockScreenWallpaper uri===============" + uri);
  9. long ident = Binder.clearCallingIdentity();
  10. Intent sendlock = new Intent();
  11. String packageName = "com.android.launcher3";
  12. String serviceClassName = packageName + ".LockScreenWallPaperService";
  13. sendlock.putExtra("path", uri);
  14. sendlock.setComponent(new ComponentName(packageName, serviceClassName));
  15. mContext.startServiceAsUser(sendlock, UserHandle.OWNER);
  16. Binder.restoreCallingIdentity(ident);
  17. return true;
  18. }

}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 中调用setLockScreenWallpaper方法启动LockScreenWallPaperService,所以设置默认wallpaper还是要由setLockScreenWallpaper实现的.下面要实现LockScreenWallPaperService了,路径为packages/apps/Launcher3/src/com/android/launcher3/LockScreenWallPaperService.javapackage 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 = “”;

  1. @Override
  2. public void onCreate() {
  3. }
  4. @Override
  5. public int onStartCommand(Intent intent, int flags, int startId) {
  6. Log.d(TAG, "onStartCommand ");
  7. if (intent != null) {
  8. path = intent.getStringExtra("path");
  9. }
  10. Bitmap bitmap = BitmapFactory.decodeFile(path);
  11. SavePicToLocal savePic = new SavePicToLocal(bitmap);
  12. savePic.execute("save picture");
  13. return START_STICKY;
  14. }
  15. public boolean dumpBitmap(Bitmap mBitmap) throws FileNotFoundException {
  16. Log.d(TAG, "dumpBitmap");
  17. boolean flagSaveCompelete = false;
  18. Bitmap bitmap_land, bitmap_port;
  19. int height = mBitmap.getHeight();
  20. int width = mBitmap.getWidth();
  21. int lswidth = 1920;
  22. int lsheight = 1200;
  23. float lper = Math.max((float) lswidth / (float) width, (float) lsheight
  24. / (float) height);
  25. if (lper > 1) {
  26. Matrix lmatrix = new Matrix();
  27. lmatrix.postScale(lper, lper);
  28. bitmap_land = Bitmap.createBitmap(mBitmap,
  29. (int) ((mBitmap.getWidth() - lswidth / lper) / 2),
  30. (int) ((mBitmap.getHeight() - lsheight / lper) / 2),
  31. (int) (lswidth / lper), (int) (lsheight / lper), lmatrix,
  32. true);
  33. } else {
  34. bitmap_land = Bitmap.createBitmap(mBitmap,
  35. (int) ((mBitmap.getWidth() - lswidth) / 2),
  36. (int) ((mBitmap.getHeight() - lsheight) / 2), lswidth,
  37. lsheight, null, true);
  38. }
  39. int pswidth = 1200;
  40. int psheight = 1920;
  41. float pper = Math.max((float) pswidth / (float) width, (float) psheight
  42. / (float) height);
  43. if (pper > 1) {
  44. Matrix pmatrix = new Matrix();
  45. pmatrix.postScale(pper, pper);
  46. bitmap_port = Bitmap.createBitmap(mBitmap,
  47. (int) ((mBitmap.getWidth() - pswidth / pper) / 2),
  48. (int) ((mBitmap.getHeight() - psheight / pper) / 2),
  49. (int) (pswidth / pper), (int) (psheight / pper), pmatrix,
  50. true);
  51. } else {
  52. bitmap_port = Bitmap.createBitmap(mBitmap,
  53. (int) ((mBitmap.getWidth() - pswidth) / 2),
  54. (int) ((mBitmap.getHeight() - psheight) / 2), pswidth,
  55. psheight, null, true);
  56. }
  57. Matrix matrix = new Matrix();
  58. matrix.postScale(0.5f, 0.5f);
  59. bitmap_land = Bitmap.createBitmap(bitmap_land, 0, 0,
  60. bitmap_land.getWidth(), bitmap_land.getHeight(), matrix, true);
  61. bitmap_port = Bitmap.createBitmap(bitmap_port, 0, 0,
  62. bitmap_port.getWidth(), bitmap_port.getHeight(), matrix, true);
  63. flagSaveCompelete = saveBitmapToFile(
  64. bitmap_port,
  65. "/data/local/tmp/lockscreenwallpaper/keyguard_wallpaper_land.png",
  66. 1);
  67. flagSaveCompelete = saveBitmapToFile(
  68. bitmap_land,
  69. "/data/local/tmp/lockscreenwallpaper/keyguard_wallpaper_port.png",
  70. 2);
  71. return flagSaveCompelete;
  72. }
  73. private boolean saveBitmapToFile(Bitmap bitmap, String path, int isRecycle)
  74. throws FileNotFoundException {
  75. Log.d(TAG, "saveBitmapToFile ident=" + "bitmap" + bitmap);
  76. boolean result = false;
  77. if (bitmap == null)
  78. return result;
  79. Bitmap tmpbm = null;
  80. java.io.FileOutputStream tmpfos = null;
  81. try {
  82. tmpbm = bitmap;
  83. tmpfos = new java.io.FileOutputStream(path);
  84. tmpbm.compress(Bitmap.CompressFormat.PNG, 100, tmpfos);
  85. Log.d(TAG, "saveBitmapToFile compress");
  86. } catch (FileNotFoundException ex) {
  87. Log.d(TAG, "ex1" + ex);
  88. throw ex;
  89. } catch (java.io.IOException ex) {
  90. Log.d(TAG, "ex2" + ex);
  91. ex.printStackTrace();
  92. } finally {
  93. if (tmpfos != null) {
  94. try {
  95. Log.d(TAG, "tmpfos.close() ");
  96. tmpfos.close();
  97. result = true;
  98. } catch (java.io.IOException ex) {
  99. Log.d(TAG, "ex3" + ex);
  100. }
  101. }
  102. if (tmpbm != null && !tmpbm.isRecycled())
  103. if (isRecycle == 2) {
  104. tmpbm.recycle();
  105. }
  106. }
  107. return result;
  108. }
  109. class SavePicToLocal extends AsyncTask<String, Integer, Boolean> {
  110. Bitmap bitmap;
  111. public SavePicToLocal(Bitmap mBitmap) {
  112. bitmap = mBitmap;
  113. }
  114. @Override
  115. protected Boolean doInBackground(String... params) {
  116. return dumpBitmaps();
  117. }
  118. private boolean dumpBitmaps() {
  119. boolean flag = false;
  120. try {
  121. flag = dumpBitmap(bitmap);
  122. flag = true;
  123. } catch (FileNotFoundException e) {
  124. }
  125. return flag;
  126. }
  127. @Override
  128. protected void onPostExecute(Boolean result) {
  129. if (result) {
  130. sendBroadcast(new Intent(
  131. "android.intent.action.UPDATE_LOCK_WALLPAPER"));
  132. Log.d(TAG, "send UPDATE_LOCK_WALLPAPER");
  133. }
  134. }
  135. @Override
  136. protected void onProgressUpdate(Integer... values) {
  137. super.onProgressUpdate(values);
  138. }
  139. }
  140. @Override
  141. public IBinder onBind(Intent intent) {
  142. return null;
  143. }

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