Android線上更新 遠程安裝程式

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   color   ar   os   使用   

原文:http://blog.csdn.net/jasper_success/article/details/7984065

第一步:使用java.net的URLConnection對象來建立串連

第二步:通過InputStream將下載的檔案寫入儲存卡內緩衝

第三步:下載完畢之後,通過自訂的openFile()方法開啟檔案,判斷檔案類型,若為APK,開始安裝

第四步:準備離開Installer程式的同時,通過自製的delFile()方法,刪除緩衝內檔案

 

/** * 遠程下載安裝Android程式 *  * @ClassName InstallOnlineActivity * @author Jet * @date 2012-9-14 */public class InstallOnlineActivity extends Activity {    private TextView mTextView;    private EditText mEditText;    private Button mButton;    private String currentFilePath = "";    private String currentTempFilePath = "";    private String strURL = "";    private String fileEx = "";    private String fileName = "";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.installonline);        mTextView = (TextView) findViewById(R.id.installonline_text1);        mEditText = (EditText) findViewById(R.id.installonline_edittext1);        mButton = (Button) findViewById(R.id.installonline_button1);        mButton.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // 將檔案下載到本地                mTextView.setText("下載中...");                strURL = mEditText.getText().toString();                // 截取檔案尾碼                fileEx = strURL.substring(strURL.lastIndexOf(‘.‘) + 1,                        strURL.length()).toLowerCase();                // 截取檔案名稱                fileName = strURL.substring(strURL.lastIndexOf(‘/‘) + 1,                        strURL.lastIndexOf(‘.‘));                getFile(strURL);            }        });    }    private void getFile(final String strPath) {        if (currentFilePath.equals(strPath)) {            getDataSource(strPath);        }        currentFilePath = strPath;        Runnable r = new Runnable() {            @Override            public void run() {                getDataSource(strPath);            }        };        new Thread(r).start();    }    private void getDataSource(String url) {        if (!URLUtil.isNetworkUrl(url)) {            mTextView.setText("請填寫正確的URL");        } else {            try {                URL myUrl = new URL(url);                // 取得串連                URLConnection conn = myUrl.openConnection();                // 串連                conn.connect();                // 獲得輸入資料流                InputStream is = conn.getInputStream();                if (is == null) {                    throw new RuntimeException("stream is null");                }                // 建立臨時檔案                File myTempFile = File.createTempFile(fileName, "." + fileEx);                // 取得臨時檔案存放路徑                currentTempFilePath = myTempFile.getAbsolutePath();                FileOutputStream fos = new FileOutputStream(myTempFile);                byte[] buf = new byte[128];                do {                    // 返回現在所讀緩衝區的大小                    int numread = is.read(buf);                    if (numread <= 0) {                        break;                    }                    fos.write(buf, 0, numread);                } while (true);                // 開啟檔案進行安裝                openFile(myTempFile);                is.close();            } catch (MalformedURLException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            }        }    }    private void openFile(File file) {        Intent intent = new Intent();        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        intent.setAction(android.content.Intent.ACTION_VIEW);        String type = getMimeType(file);        intent.setDataAndType(Uri.fromFile(file), type);        startActivity(intent);    }    private String getMimeType(File file) {        String type = "";        String fname = file.getName();        // 獲得副檔名        String end = fname                .substring(fname.lastIndexOf(‘.‘) + 1, fname.length())                .toLowerCase();        // 按副檔名的類型決定MimeType        if ("m4a".equals(end) || "mp3".equals(end) || "mid".equals(end)                || "xmf".equals(end) || "ogg".equals(end) || "wav".equals(end)) {            type = "audio";        } else if ("3gp".equals(end) || "mp4".equals(end)) {            type = "video";        } else if ("jpg".equals(end) || "gif".equals(end) || "png".equals(end)                || "jpeg".equals(end) || "bmp".equals(end)) {            type = "image";        } else if ("apk".equals(end)) {            type = "application/vnd.android.package-archive";        } else {            type = "*";        }        if ("apk".equals(end)) {        } else {            type += "/*";        }        return type;    }    private void delFile(String fileName){        File file = new File(fileName);        if(file.exists()){            file.delete();        }    }    @Override    protected void onPause() {        mTextView = (TextView) findViewById(R.id.installonline_text1);        mTextView.setText("下載成功");        super.onPause();    }    @Override    protected void onResume() {        //刪除臨時檔案        delFile(currentTempFilePath);        super.onResume();    }}

 

Android線上更新 遠程安裝程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.