解决内部存储空间紧张,不加载桌面壁纸,桌面壁纸显示

说明:

当内部存储空间不足的情况下不加载壁纸,以节省资源。


修改方式:在WallpaperManagerService.java中进行修改;

思路:首先在加载壁纸之前我们需要判断当前存储空间是否紧张。代码如下:( 源码地址

public boolean isStorageLow(){
  try{
      if(mIpackageManager!=null){
          return mIPackageManager.isStorageLow();
      }

  }catch(RemoteException e){
    
  }
  return false;
}



     boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force,
0813             boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) {
0814         if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName);

if(isStorageLow()){
    return false;
}

0815         // Has the component changed?
0816         if (!force) {
0817             if (wallpaper.connection != null) {
0818                 if (wallpaper.wallpaperComponent == null) {
0819                     if (componentName == null) {
0820                         if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default");
0821                         // Still using default wallpaper.
0822                         return true;
0823                     }
0824                 } else if (wallpaper.wallpaperComponent.equals(componentName)) {
0825                     // Changing to same wallpaper.
0826                     if (DEBUG) Slog.v(TAG, "same wallpaper");
0827                     return true;
0828                 }
0829             }
0830         }
0831         
0832         try {
0833             if (componentName == null) {
0834                 String defaultComponent = 
0835                     mContext.getString(com.android.internal.R.string.default_wallpaper_component);
0836                 if (defaultComponent != null) {
0837                     // See if there is a default wallpaper component specified
0838                     componentName = ComponentName.unflattenFromString(defaultComponent);
0839                     if (DEBUG) Slog.v(TAG, "Use default component wallpaper:" + componentName);
0840                 }
0841                 if (componentName == null) {
0842                     // Fall back to static image wallpaper
0843                     componentName = IMAGE_WALLPAPER;
0844                     //clearWallpaperComponentLocked();
0845                     //return;
0846                     if (DEBUG) Slog.v(TAG, "Using image wallpaper");
0847                 }
0848             }
0849             int serviceUserId = wallpaper.userId;
0850             ServiceInfo si = mIPackageManager.getServiceInfo(componentName,
0851                     PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId);
0852             if (si == null) {
0853                 // The wallpaper component we're trying to use doesn't exist
0854                 Slog.w(TAG, "Attempted wallpaper " + componentName + " is unavailable");
0855                 return false;
0856             }
0857             if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
0858                 String msg = "Selected service does not require "
0859                         + android.Manifest.permission.BIND_WALLPAPER
0860                         + ": " + componentName;
0861                 if (fromUser) {
0862                     throw new SecurityException(msg);
0863                 }
0864                 Slog.w(TAG, msg);
0865                 return false;
0866             }
0867             
0868             WallpaperInfo wi = null;
0869             
0870             Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
0871             if (componentName != null && !componentName.equals(IMAGE_WALLPAPER)) {
0872                 // Make sure the selected service is actually a wallpaper service.
0873                 List<ResolveInfo> ris =
0874                         mIPackageManager.queryIntentServices(intent,
0875                                 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
0876                                 PackageManager.GET_META_DATA, serviceUserId);
0877                 for (int i=0; i<ris.size(); i++) {
0878                     ServiceInfo rsi = ris.get(i).serviceInfo;
0879                     if (rsi.name.equals(si.name) &&
0880                             rsi.packageName.equals(si.packageName)) {
0881                         try {
0882                             wi = new WallpaperInfo(mContext, ris.get(i));
0883                         } catch (XmlPullParserException e) {
0884                             if (fromUser) {
0885                                 throw new IllegalArgumentException(e);
0886                             }
0887                             Slog.w(TAG, e);
0888                             return false;
0889                         } catch (IOException e) {
0890                             if (fromUser) {
0891                                 throw new IllegalArgumentException(e);
0892                             }
0893                             Slog.w(TAG, e);
0894                             return false;
0895                         }
0896                         break;
0897                     }
0898                 }
0899                 if (wi == null) {
0900                     String msg = "Selected service is not a wallpaper: "
0901                             + componentName;
0902                     if (fromUser) {
0903                         throw new SecurityException(msg);
0904                     }
0905                     Slog.w(TAG, msg);
0906                     return false;
0907                 }
0908             }
0909             
0910             // Bind the service!
0911             if (DEBUG) Slog.v(TAG, "Binding to:" + componentName);
0912             WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper);
0913             intent.setComponent(componentName);
0914             intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
0915                     com.android.internal.R.string.wallpaper_binding_label);
0916             intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser(
0917                     mContext, 0,
0918                     Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER),
0919                             mContext.getText(com.android.internal.R.string.chooser_wallpaper)),
0920                     0, null, new UserHandle(serviceUserId)));
0921             if (!mContext.bindServiceAsUser(intent, newConn,
0922                     Context.BIND_AUTO_CREATE | Context.BIND_SHOWING_UI,
0923                     new UserHandle(serviceUserId))) {
0924                 String msg = "Unable to bind service: "
0925                         + componentName;
0926                 if (fromUser) {
0927                     throw new IllegalArgumentException(msg);
0928                 }
0929                 Slog.w(TAG, msg);
0930                 return false;
0931             }
0932             if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null) {
0933                 detachWallpaperLocked(mLastWallpaper);
0934             }
0935             wallpaper.wallpaperComponent = componentName;
0936             wallpaper.connection = newConn;
0937             wallpaper.lastDiedTime = SystemClock.uptimeMillis();
0938             newConn.mReply = reply;
0939             try {
0940                 if (wallpaper.userId == mCurrentUserId) {
0941                     if (DEBUG)
0942                         Slog.v(TAG, "Adding window token: " + newConn.mToken);
0943                     mIWindowManager.addWindowToken(newConn.mToken,
0944                             WindowManager.LayoutParams.TYPE_WALLPAPER);
0945                     mLastWallpaper = wallpaper;
0946                 }
0947             } catch (RemoteException e) {
0948             }
0949         } catch (RemoteException e) {
0950             String msg = "Remote exception for " + componentName + "\n" + e;
0951             if (fromUser) {
0952                 throw new IllegalArgumentException(msg);
0953             }
0954             Slog.w(TAG, msg);
0955             return false;
0956         }
0957         return true;
0958     }

注!

红色部分为增加代码。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高桐@BILL

分享快乐,快乐分享...

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值