Android 7.0 MTK Set Default Desktop

  • 2021-11-10 10:47:03
  • OfStack

This article example for everyone to share Android7.0 MTK set the default desktop specific code, for your reference, the specific content is as follows

Project Requirements: Customers install their own desktop apk, and automatically set it as the default desktop after installation, and do not pop up the always and only once pop-up box

1. Find the file to install the application


frameworks\base\services\core\java\com\android\server\pm\PackageManagerService.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);
 //  Due to the confidentiality of customers' applications,   Use the name of the bag on the dessert table here ---com.dianxinos.dxhome
 if (pkgName.equals("com.dianxinos.dxhome")){
  // Send a broadcast 
  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. Since the default desktop is set in Settings, broadcast recipients add and set the default desktop in settings (add files under packages\ apps\ Settings\ src\ com\ android\ settings-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();

   //  Package name and class name of snack desktop  com.dianxinos.dxhome / com.nd.hilauncherdev.launcher.Launcher Set the default desktop 
 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);
   
 // Refresh the desktop 
  Intent intent2 = new Intent(Intent.ACTION_MAIN);
   intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   intent2.addCategory(Intent.CATEGORY_HOME);
   context.startActivity(intent2);
 }
 }
}

3.packages\apps\Settings\AndroidManifest.xml


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

Related articles: