Recently Tinker Android, encountered a problem, because there is no root permissions, need to invoke intent installation apk, but need to get the user is installed (such as, the user clicked back or cancel), check a lot of articles, and finally can be resolved, but there are flaws, the solution is as follows:
public static void InstallPackage (context, file, int requestcode) {//before installation records the name of the preinstallation apk file to be used to return results based on the REQUESTC
Ode gets the installed apk packagename MainActivity.m_mapInstallApkData.put (Requestcode, File.getname ());
GXJ: If the main interface is not displayed, you need to bring up the main interface to the foreground Intent it1 = new Intent (context, mainactivity.class); It1.setflags (Intent.flag_activity_new_task |
intent.flag_activity_reset_task_if_needed);
Context.startactivity (IT1); GXJ: Do not use flag_activity_new_task because you cannot get result, but also to maintain multiple apk (Task) Installations (flag_activity_single_top/flag_activity_ Previous_is_top)//For the use of Flag_activity_single_top/flag_activity_previous_is_top, Startactivityforresult only returns when all the installation interfaces are gone, and the return ResultCode is 0, so it is doubtful that you need to check the success of the installation if you return it intent.flag_activity_ New_task is because Intent.flag_activity_new_task cannot get the result returned; Intent.flag_activity_clear_top cannot be used because more than one apk can be installed at the same time Intent =
New Intent (); It2.setflags (Intent.flag_activity_single_top);
or Flag_activity_previous_is_top it2.setaction (Android.content.Intent.ACTION_VIEW); Uri Path = URi.fromfile (file);
It2.setdataandtype (Path, "application/vnd.android.package-archive");
Context.startactivityforresult (it2, Requestcode); }
Add protected void Onactivityresult (int requestcode, int resultcode, Intent data) processing in mainactivity
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
String strpackagename = null;
if (m_mapinstallapkdata!= null)
Strpackagename = M_mapinstallapkdata.remove (requestcode);
Do not know why, whether or not to click on installation, cancellation, the return of the ResultCode are 0, that is result_canceled, the unknown
if (ResultCode = = result_canceled) {
if (!) Textutils.isempty (Strpackagename)) {
//need to re-examine whether the PackageName is installed//
. Do your own Processing
}}}
Note: The program has a flaw, that is, if the installation of APK box A and apk box B, click B's Cancellation no response, only in a, B, the Installation box disappears (click Install or Cancel or return) will have return (respectively returns the results of A and B), the solution has not been found, pending