如何在程式碼中執行apk安裝

jia635發表於2014-08-19
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);  


		// package 一定要有 後面是包名 ,系統軟體不能解除安裝
				MainActivity.this.startActivity(intent2);
			}
		});
    }
}

相關文章