0
点赞
收藏
分享

微信扫一扫

Android之应用程序自动升级 下载后…

杨沐涵 2023-07-31 阅读 48



<uses-permission android:name=”android.permission.INSTALL_PACKAGES” />,这样你的APP就有安装软件权限了。
接下是安装的关键代码,下载完后执行:


Uri uri = Uri.fromFile(new File("/sdcard/temp.apk")); //这里是APK路径
 Intent intent = new Intent(Intent.ACTION_VIEW);
 intent.setDataAndType(uri,"application/vnd.android.package-archive");
 startActivity(intent);
//网络下载apk文件然后安装
public class Main extends Activity implements OnClickListener
 {
  private void installApk(String filename)
  {
   File file = new File(filename);
   Intent intent = new Intent();
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   intent.setAction(Intent.ACTION_VIEW);
   String type = "application/vnd.android.package-archive";
   intent.setDataAndType(Uri.fromFile(file), type);
   startActivity(intent);}
@Override
  public void onClick(View view)
  {
   String downloadPath = Environment.getExternalStorageDirectory().getPath() + "/download_cache";   String url = "http://192.168.1.55/xinlangweibo_1_0.apk";
   File file = new File(downloadPath);
   if(!file.exists())
   {
    file.mkdir();
   }
   HttpGet httpGet = new HttpGet(url);
   try
   {
    HttpResponse httpResponse = new DefaultHttpClient()
      .execute(httpGet);
    if (httpResponse.getStatusLine().getStatusCode() == 200)
    {   InputStream is = httpResponse.getEntity().getContent();
     FileOutputStream fos = new FileOutputStream(downloadPath
       + "/integration.apk");
     byte[] buffer = new byte[8192];
     int count = 0;
     while ((count = is.read(buffer)) != -1)
     {
      fos.write(buffer, 0, count);
     }
     fos.close();
     is.close();
     installApk(downloadPath+ "/integration.apk");
    }
   }
   catch (Exception e)
   {
   }}
@Override
  public void onCreate(Bundle savedInstanceState)
  {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main); 
   Button btnDownloadInstallApk = (Button) findViewById(R.id.btnDownloadInstallApk);
   btnDownloadInstallApk.setOnClickListener(this);
   
  }

举报

相关推荐

0 条评论