Android app background installation, Silent Installation Code Implementation Method

Source: Internet
Author: User

First, you need to have a Linux environment or other environment that can be Mmm cross-compiled, because this is used to hide the API, which is not available in the SDK. The ecilipse will prompt that the package does not exist, but cross-compilation is acceptable. If you do not know

 

import android.app.Activity;import android.os.Bundle;import android.content.Intent;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.content.pm.PackageManager.NameNotFoundException;import android.content.pm.IPackageInstallObserver;import android.content.res.Resources;import android.graphics.drawable.Drawable;import android.net.Uri;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ProgressBar;import android.widget.TextView;public class InstallActivity extends Activity {    /** Called when the activity is first created. */    private Button btn2=null;    private final int INSTALL_COMPLETE = 1;    final static int SUCCEEDED = 1;    final static int FAILED = 0;    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        btn=(Button) findViewById(R.id.btn2);        btn2.setOnClickListener(listener2);} private OnClickListener listener2=new  OnClickListener(){        public void onClick(View v) {            String fileName = Environment.getExternalStorageDirectory() +  "/baidu/"+"UC.apk";             Uri uri = Uri.fromFile(new File(fileName));            int installFlags = 0;            PackageManager pm = getPackageManager();            try {                PackageInfo pi = pm.getPackageInfo("com.uc.browser",                         PackageManager.GET_UNINSTALLED_PACKAGES);                if(pi != null) {                    installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;                }            } catch (NameNotFoundException e) {            }//            String installerPackageName = getIntent().getStringExtra(//                    Intent.EXTRA_INSTALLER_PACKAGE_NAME);                        PackageInstallObserver observer = new PackageInstallObserver();            pm.installPackage(uri, observer, installFlags, "com.uc.browser");         }    };        class PackageInstallObserver extends IPackageInstallObserver.Stub {        public void packageInstalled(String packageName, int returnCode) {            Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);            msg.arg1 = returnCode;            mHandler.sendMessage(msg);        }    };        private Handler mHandler = new Handler() {        public void handleMessage(Message msg) {            switch (msg.what) {                case INSTALL_COMPLETE:                    // Update the status text                    // Show the ok button                    int centerTextLabel;                    Drawable centerTextDrawable = null;                    if(msg.arg1 == SUCCEEDED) {                                            } else {}                    if (centerTextDrawable != null) {}                    break;                default:                    break;            }        }    };   }

The key point is to implement packageinstallobserver. In addition, pay attention to import Android. content. PM ....... The packages must be imported. The android. mk file is as follows:

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := userLOCAL_SRC_FILES := $(call all-subdir-java-files)LOCAL_PACKAGE_NAME := InstallActivityLOCAL_CERTIFICATE := platforminclude $(BUILD_PACKAGE)

The key point is local_certificate: = platform.

In addition, the R. Java class repetition error may be reported during compilation. You can delete the gen folder in your project without affecting compilation. It is said that ^… will be automatically generated elsewhere ......

This can be used for compiling on 2.1, background installation, and background uninstallation.

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.