[Description]
How to automatically install the APK on the external SD card to the memory card of the mobile phone when the mobile phone is started for the first time.
[Solution]
Design Concept:
1.
Put the APK to a directory on the SD card, suchApks_preinstallDirectory.
(For those APK whose storage path is internalonly in androidmanifest. XML, this installation method is not recommended)
2. During the first boot, wait until the SD card is mounted and read the directory to install APK.
Implementation Code:
1. Modify\ Packages \ apps \ packageinstaller \ androidmanifest. xml
<! -- Add a receiver -->
<Cycler Android: Name = ". firstinstallreceiver">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. media_mounted_for_firtime"/>
</Intent-filter>
</Cycler>
<! -- End
-->
2.\ Packages \ apps \ packageinstaller \ SRC \ com \ Android \ packageinstallerAddFirstinstallreceiver. JavaFile, used to receive the broadcast from the first SD card mount for APK installation. The content is as follows:
Package com. Android. packageinstaller;
Import Android. content. contentvalues;
Import Android. content. context;
Import Android. content. intent;
Import Android. content. broadcastreceiver;
Import Android. content. intentfilter;
Import Android. content. sharedpreferences;
Import Android. util. log;
Import Android. content. PM. ipackageinstallobserver;
Import Android. content. PM. packageinfo;
Import Android. content. PM. packagemanager;
Import java. Io. file;
Import android.net. Uri;
Public class firstinstallreceiver extends broadcastreceiver {
Private final string tag = "firstinstallreceiver ";
Private string action = "android. Intent. Action. media_mounted_for_firtime ";
/* The following Variable apkspath is a path on the external SD card,
To modify the default APK storage path, modify this path.
GB version, GB2, ICS single-Card Project: SD card path is/mnt/sdcard
GB2 dual-Card Project:
The default external SD card path is/mnt/sdcard/sdcard2,
If sdswap feature is enabled, the path of the external SD card is/mnt/sdcard.
ICS dual-Card Project:
The default external SD card path is/mnt/sdcard2,
If sdswap feature is enabled, the path of the external SD card is/mnt/sdcard.
Therefore, to modify the path of the external SD card, you must first confirm the path of the external SD card.
*/
Private string apkspath = "/mnt/sdcard/apks_preinstall ";
// Assume that the path of the external SD card is/mnt/sdcard /////////////////////////// //////////////////////////////////////// /////////////////////////////////
@ Override
Public void
Onreceive (context, intent ){
Log. D (TAG, "onreceive:" + intent );
New thread ()
{
Public void run (){
Try
{
File apksfile = new file (apkspath );
Packagemanager PM = context. getpackagemanager ();
Int installflags = 0;
Installflags | = packagemanager. install_external;
If (apksfile. exists ()){
File [] files = apksfile. listfiles ();
For (File F1: files)
{
String filename = f1.getname ();
String type = filename. substring (filename. lastindexof (".") + 1 );
If (type! = NULL & type. Equals ("APK "))
{
Uri mpackageuri = URI. fromfile (F1 );
PM. installpackage (mpackageuri, null, installflags, null );
}
}
}
}
Catch (exception E)
{
Log. D (TAG, "onreceive: Install failed: e:" + E );
}
}
}. Start ();
}
}
3. ModifyMountservice. JavaUsed to send the broadcast of the first boot SD card Mount
Private void policyvolumestatechange (string label, string path, int oldstate, int newstate ){.....
Else if (newstate = volumestate. mounted ){
// MTK
Added
String isfirstboot = systemproperties. Get ("Persist. SYS. mount_is_first ");
If (isfirstboot = NULL | isfirstboot. Equals (""))
{
Slog. I (TAG, "first boot ");
Systemproperties. Set ("Persist. SYS. mount_is_first", "done ");
Intent firintent = new intent ("android. Intent. Action. media_mounted_for_firtime ");
Mcontext. sendstickybroadcast (firintent );
}
// MTK
Added
If (debug_events) slog. I (TAG, "Updating volume State mounted ");
Updatepublicvolumestate (path, environment. media_mounted );
.....