2013-09-24 69 views
2

我正在使用以下程序下載並安裝apk。如何在成功安裝後從安裝SD卡中刪除apk?我試過的是:如何在安裝後刪除.apk

File file = new File("/sdcard/filename.apk"); 
boolean deleted = file.delete(); 

但是它會在安裝前刪除文件。

protected Boolean doInBackground(String... arg0) { 
    try { 
    URL url = new URL(weburl +"username="+usename+"&password="+usepass +"&apk="+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(); 
    // this.checkUnknownSourceEnability(); 
    // this.initiateInstallation(); 
    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); 

    } catch (MalformedURLException e) { 
    e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
    return null; 
} 

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); 
} 
+0

可能重複[如何找出何時完成安裝(http://stackoverflow.com/questions/5176645/how-to-find-out - 當安裝完成時) – flx

+1

安裝後刪除應用程序(\ *。apk)的可能的重複](http://stackoverflow.com/questions/15984546/delete-an-application-apk-after -安裝) –

回答