首页 > 代码库 > 如何在代码中执行apk安装

如何在代码中执行apk安装

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
          
        Button installBtn = (Button)findViewById(R.id.install_btn);
        installBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setDataAndType(Uri.fromFile(new File("/sdcard/xxx.apk")), 
						"application/vnd.android.package-archive");
				
				startActivity(intent);
			}
		});
        
        Button uninstallBtn = (Button)findViewById(R.id.uninstall_btn);
        uninstallBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse("package:com.xxx.xxx(包名)");
				Intent intent2 = new Intent(Intent.ACTION_DELETE, uri);  
<span style="white-space:pre">				</span>// package 一定要有 后面是包名 ,系统软件不能卸载
				MainActivity.this.startActivity(intent2);
			}
		});
    }
}