我是靠谱客的博主 英俊路灯,最近开发中收集的这篇文章主要介绍Android7.0 MTK设置默认桌面,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

本文实例为大家分享了Android7.0 MTK设置默认桌面的具体代码,供大家参考,具体内容如下

项目需求:客户安装自己公司的桌面apk,安装完成后自动设置为默认桌面且不弹出始终和仅一次的弹框

1.找到安装应用的文件

frameworksbaseservicescorejavacomandroidserverpmPackageManagerService.java

 try {
   PackageParser.Package newPackage = scanPackageTracedLI(pkg, policyFlags, scanFlags,
     System.currentTimeMillis(), user);

   updateSettingsLI(newPackage, installerPackageName, null, res, user);

   if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) {
 
 
    prepareAppDataAfterInstallLIF(newPackage);
 

 Log.d("yh", "pkgName " +pkgName);
 // 由于客户的应用保密 这里使用点心桌面的包名---com.dianxinos.dxhome
 if (pkgName.equals("com.dianxinos.dxhome")){
  //发送广播
  Intent intent = new  Intent("android.intent.action.UPDATE_LANUCHER_APPS");
  mContext.sendBroadcast(intent);
 }
    //------------------------


   } else {
    // Remove package from internal structures, but keep around any
    // data that might have already existed
    deletePackageLIF(pkgName, UserHandle.ALL, false, null,
      PackageManager.DELETE_KEEP_DATA, res.removedInfo, true, null);
   }
  } catch (PackageManagerException e) {
   res.setError("Package couldn't be installed in " + pkg.codePath, e);
  }

  Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
 }

2.由于设置默认桌面时,是在Settings中设置的所以广播接收者在settings中添加并设置默认桌面(添加文件–packagesappsSettingssrccomandroidsettings下—UpdateLanucherReceiver.java)

package com.android.settings;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.ComponentName;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import java.util.ArrayList;
import android.util.Log;
import android.content.pm.ActivityInfo;
import java.util.List;


public class UpdateLanucherReceiver extends BroadcastReceiver {
 
 public UpdateLanucherReceiver() {
 }
 
 @Override
 public void onReceive(Context context, Intent intent) {
  if (intent.getAction().equals("android.intent.action.UPDATE_LANUCHER_APPS")) {
 Log.e("yhyh" ," onReceive");
 
 final PackageManager mPm = context.getPackageManager();

   // 点心桌面的包名类名 com.dianxinos.dxhome / com.nd.hilauncherdev.launcher.Launcher设置默认桌面
 ComponentName DefaultLauncher=new ComponentName(" com.dianxinos.dxhome",
     "com.nd.hilauncherdev.launcher.Launcher");
   ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
   ComponentName currentDefaultHome = mPm.getHomeActivities(homeActivities);

   ComponentName[]mHomeComponentSet = new ComponentName[homeActivities.size()];
   for (int i = 0; i < homeActivities.size(); i++) {
    final ResolveInfo candidate = homeActivities.get(i);
    Log.e("yhyh","homeActivitie: candidate = "+candidate);
    final ActivityInfo activityInfo= candidate.activityInfo;
    ComponentName activityName = new ComponentName(activityInfo.packageName, activityInfo.name);
    mHomeComponentSet[i] = activityName;
   }
   IntentFilter mHomeFilter = new IntentFilter(Intent.ACTION_MAIN);
   mHomeFilter.addCategory(Intent.CATEGORY_HOME);
   mHomeFilter.addCategory(Intent.CATEGORY_DEFAULT);
   List<ComponentName>Activities=new ArrayList();
   mPm.replacePreferredActivity(mHomeFilter, IntentFilter.MATCH_CATEGORY_EMPTY,mHomeComponentSet, DefaultLauncher);
   
 //刷新桌面
  Intent intent2 = new Intent(Intent.ACTION_MAIN);
   intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   intent2.addCategory(Intent.CATEGORY_HOME);
   context.startActivity(intent2);
 }
 }
}

3.packagesappsSettingsAndroidManifest.xml

<!-- yh -->
 <receiver
   android:name=".UpdateLanucherReceiver" >
   <intent-filter>
    <action android:name="android.intent.action.UPDATE_LANUCHER_APPS" />
   </intent-filter>
</receiver>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是英俊路灯为你收集整理的Android7.0 MTK设置默认桌面的全部内容,希望文章能够帮你解决Android7.0 MTK设置默认桌面所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部