0

在我的Android应用程序中,我有1个按钮来下载.apk,然后即时通讯添加另一个来安装.apk,当您单击下载按钮时,它会从我的网站下载.apk文件,在你的SD卡存储的根目录下!我必须添加什么,所以当用户点击名为“安装”的按钮时,他们需要安装屏幕?下载并安装Android应用程序

MainActvity.java

package cydia.jb.download; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import android.app.Activity; 
import android.app.Dialog; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.View; 
import android.view.Window; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

ProgressBar pb; 
Dialog dialog; 
int downloadedSize = 0; 
int totalSize = 0; 
TextView cur_val; 
String dwnload_file_path = "http://diordnakclab.net23.net/cydia/Cydia.apk"; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button b = (Button) findViewById(R.id.button1); 
    b.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      showProgress(dwnload_file_path); 

       new Thread(new Runnable() { 
        public void run() { 
         downloadFile(); 
        } 
        }).start(); 
     } 
    }); 
} 

void downloadFile(){ 

    try { 
     URL url = new URL(dwnload_file_path); 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setRequestMethod("GET"); 
     urlConnection.setDoOutput(true); 

     //connect 
     urlConnection.connect(); 

     //set the path where we want to save the file   
     File SDCardRoot = Environment.getExternalStorageDirectory(); 
     //create a new file, to save the downloaded file 
     File file = new File(SDCardRoot,"Cydia.apk"); 

     FileOutputStream fileOutput = new FileOutputStream(file); 

     //Stream used for reading the data from the internet 
     InputStream inputStream = urlConnection.getInputStream(); 

     //this is the total size of the file which we are downloading 
     totalSize = urlConnection.getContentLength(); 

     runOnUiThread(new Runnable() { 
      public void run() { 
       pb.setMax(totalSize); 
      }    
     }); 

     //create a buffer... 
     byte[] buffer = new byte[1024]; 
     int bufferLength = 0; 

     while ((bufferLength = inputStream.read(buffer)) > 0) { 
      fileOutput.write(buffer, 0, bufferLength); 
      downloadedSize += bufferLength; 
      // update the progressbar // 
      runOnUiThread(new Runnable() { 
       public void run() { 
        pb.setProgress(downloadedSize); 
        float per = ((float)downloadedSize/totalSize) * 100; 
        cur_val.setText("Downloaded " + downloadedSize + "KB/" + totalSize + "KB (" + (int)per + "%)"); 
       } 
      }); 
     } 
     //close the output stream when complete // 
     fileOutput.close(); 
     runOnUiThread(new Runnable() { 
      public void run() { 
       // pb.dismiss(); // if you want close it.. 
      } 
     });   

    } catch (final MalformedURLException e) { 
     showError("Error : MalformedURLException " + e);   
     e.printStackTrace(); 
    } catch (final IOException e) { 
     showError("Error : IOException " + e);   
     e.printStackTrace(); 
    } 
    catch (final Exception e) { 
     showError("Error : Please check your internet connection " + e); 
    }  
} 

void showError(final String err){ 
    runOnUiThread(new Runnable() { 
     public void run() { 
      Toast.makeText(MainActivity.this, err, Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

void showProgress(String file_path){ 
    dialog = new Dialog(MainActivity.this); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.myprogressdialog); 
    dialog.setTitle("Download Progress"); 

    TextView text = (TextView) dialog.findViewById(R.id.tv1); 
    text.setText("Downloading file from ... " + file_path); 
    cur_val = (TextView) dialog.findViewById(R.id.cur_pg_tv); 
    cur_val.setText("Starting download..."); 
    dialog.show(); 

    pb = (ProgressBar)dialog.findViewById(R.id.progress_bar); 
    pb.setProgress(0); 
    pb.setProgressDrawable(getResources().getDrawable(R.drawable.green_progress)); 
} 
} 

CydiaManafest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="cydia.jb.download" 
android:versionCode="4" 
android:versionName="4.0" > 

<uses-sdk 
    android:minSdkVersion="16" 
    android:targetSdkVersion="17" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="cydia.jb.download.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 
+0

打开该文件,并使用意图调用安装程序包 – Metalhead1247

+0

@ Metalhead1247我不知道如何编写这种代码.. –

+0

因此,用户从Play商店下载您的应用程序,以便他们可以从您的应用程序下载您的应用程序?我头疼。另外,请注意[Google Play开发者计划政策](https://play.google.com/about/developer-content-policy.html)(请参阅:危险产品)。 –

回答

1

这里是代码

button1 = (Button) findViewById(R.id.button1); 

button1.setOnClickListener(new OnClickListener() 
{ 
    public void onClick(View v1) 
    { 
     Intent promptInstall = new Intent(Intent.ACTION_VIEW) 
      .setData(Uri.parse("file:///path/to/your.apk")) 
      .setType("application/vnd.android.package-archive"); 
     startActivity(promptInstall); 
    } 
} 
+0

应该没有按钮代码来定义按钮安装它? –

+0

这没有在编辑器中显示任何错误,但是当我在我的设备中运行该应用程序的安装按钮不起作用..有没有办法让用户在下载后提示安装?像自动? –

+0

在showprogress中添加一个if循环来检查下载是否完成。如果完整地使用你的apk路径的上述意图。你有没有设置正确的路径? – Metalhead1247