Android get Android install apk box installation status (such as click to Cancel, return)

Source: Internet
Author: User

Recently Tinker Android, encountered a problem, because there is no root permissions, need to call intent install APK, but need to get the user installed (for example, the user clicked back or cancel), checked a lot of articles, finally can be resolved, but flawed, the solution is as follows:

 Public Static voidInstallPackage (Context context, file file,intRequestcode) {    //Log the pre-installed APK file name before installation to get the installed APK PackageName based on Requestcode when returning resultsMainActivity.m_mapInstallApkData.put (Requestcode, File.getname ()); //GXJ: If the main interface is not displayed, you need to bring up the main interface to the foregroundIntent it1 =NewIntent (context, mainactivity.class); It1.setflags (Intent.flag_activity_new_task|intent.flag_activity_reset_task_if_needed);        Context.startactivity (IT1); //gxj: <span style= "color: #ff0000;" > does not use flag_activity_new_task because it cannot get result</span>, while maintaining multiple apk (Task) Installations (flag_activity_single_top/flag_ Activity_previous_is_top)//<span style= "color: #ff0000;" > 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 of the ResultCode are 0, puzzled, so after return need to check whether the installation of success judgment </span>//Intent.flag_activity_new_task cannot be used because Intent.flag_activity_new_task cannot get the returned result; Intent.flag_activity_clear_ cannot be used Top is because there may be multiple apk installed at the same timeIntent it2 =NewIntent ();    It2.setflags (Intent.flag_activity_single_top); //or Flag_activity_previous_is_topit2.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) to Mainactivity

protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {String Strpackagename=NULL; if(M_mapinstallapkdata! =NULL) Strpackagename=M_mapinstallapkdata.remove (Requestcode); //do not know why, whether or not click Install, Cancel, the return of the ResultCode are 0, that is, result_canceled, unknown origin    if(ResultCode = =result_canceled) {        if(!Textutils.isempty (Strpackagename)) {            //need to re-check if the PackageName is installed//.. Do your own processing.        }    }}

Note: The program has a flaw, that is, if the pop-up installed apk box A and apk frame B, click on the cancellation of B no response, only in a, B, the Installation box disappears (click Install or Cancel or return) will return (the results of A and B are returned respectively), the solution has not been found, pending

Android get Android install apk box installation status (such as click to Cancel, return)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.