新增下載檔案


private void downloadapk(){
    try {
        URL url = new URL("http://www.efimoto.com/nilesh/HelloWorldProject.apk");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();
        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard, "filename.apk");
        FileOutputStream fileOutput = new FileOutputStream(file);
        InputStream inputStream = urlConnection.getInputStream();
        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
            fileOutput.write(buffer, 0, bufferLength);
        }
        fileOutput.close();
    } catch (MalformedURLException e) {
            e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }
    }
<


安裝APK File

 private void installApk(){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(new File("/sdcard/filename.apk"));
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        startActivity(intent);
    }

參考網址:  請點我

arrow
arrow
    文章標籤
    Android
    全站熱搜
    創作者介紹
    創作者 笨海豚 的頭像
    笨海豚

    笨海豚部落格

    笨海豚 發表在 痞客邦 留言(0) 人氣()