天天看點

Android代碼實作APK檔案的安裝與解除安裝

安裝:

string str = "/canavacancel.apk";

string filename = environment.getexternalstoragedirectory() + str;

intent intent = new intent(intent.action_view);

 intent.setdataandtype(uri.fromfile(new file(filename)), "application/vnd.android.package-archive");

startactivity(intent);

解除安裝:

uri packageuri = uri.parse("package:com.demo.canavacancel");   

intent uninstallintent = new intent(intent.action_delete, packageuri);   

startactivity(uninstallintent);

environment擁有一些可以擷取環境變量的方法 

package:com.demo.canavacancel 這個形式是 package:程式完整的路徑 (包名+程式名).

//下載下傳apk程式代碼

protected file downloadfile(string httpurl) {

                // todo auto-generated method stub

                final string filename = "updata.apk";

                file tmpfile = new file("/sdcard/update");

                if (!tmpfile.exists()) {

                        tmpfile.mkdir();

                }

                final file file = new file("/sdcard/update/" + filename);

                try {

                        url url = new url(httpurl);

                        try {

                                httpurlconnection conn = (httpurlconnection) url

                                                .openconnection();

                                inputstream is = conn.getinputstream();

                                fileoutputstream fos = new fileoutputstream(file);

                                byte[] buf = new byte[256];

                                conn.connect();

                                double count = 0;

                                if (conn.getresponsecode() >= 400) {

                                        toast.maketext(main.this, "連接配接逾時", toast.length_short)

                                                        .show();

                                } else {

                                        while (count <= 100) {

                                                if (is != null) {

                                                        int numread = is.read(buf);

                                                        if (numread <= 0) {

                                                                break;

                                                        } else {

                                                                fos.write(buf, 0, numread);

                                                        }

                                                } else {

                                                        break;

                                                }

                                        }

                                }

                                conn.disconnect();

                                fos.close();

                                is.close();

                        } catch (ioexception e) {

                                // todo auto-generated catch block

                                e.printstacktrace();

                        }

                } catch (malformedurlexception e) {

                        // todo auto-generated catch block

                        e.printstacktrace();

                return file;

        }

//打開apk程式代碼

private void openfile(file file) {

                log.e("openfile", file.getname());

                intent intent = new intent();

                intent.addflags(intent.flag_activity_new_task);

                intent.setaction(android.content.intent.action_view);

                intent.setdataandtype(uri.fromfile(file),

                                "application/vnd.android.package-archive");

                startactivity(intent);



繼續閱讀