网络下载apk自动安装小例子

package feng.f8_6.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class HttpActivity extends Activity {

	private Button btn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		btn = (Button) findViewById(R.id.button1);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new Thread() {
					public void run() {
						// 地址网上可用哦
						String path = "http://res.neng.com/res/apk_sp/20120803/8124_com.mobi.filemanager.activity_163508.apk";
//						String path = "http://192.168.1.62:8080/MyHttpTest.apk";
						try {
							URL url = new URL(path);
							HttpURLConnection conn = (HttpURLConnection) url
									.openConnection();
							if (HttpURLConnection.HTTP_OK != conn
									.getResponseCode()) {
								Message message = Message.obtain();
								message.what = 1;
								handler.sendMessage(message);
							} else {
								if (Environment.getExternalStorageState()
										.equals(Environment.MEDIA_UNMOUNTED)) {
									 Message message=Message.obtain();
									 message.what=2;
									 handler.sendMessage(message);
								} else {
									// System.out.println("获取信息的长度:"+conn.getContentLength());
									File file = new File(Environment
											.getExternalStorageDirectory()
											+ "/feng");
									if (!file.exists()) {
										file.mkdir();
									}
									// System.out.println("file.getPath():"
									// + file.getPath());
									// System.out.println("getContentLength:"
									// + conn.getContentLength());
									File cfile = new File(file.getPath(),
											"activity_163508.apk");
									if (!cfile.exists()) {
										cfile.createNewFile();
									}
									InputStream is = conn.getInputStream();
									FileOutputStream os = new FileOutputStream(
											cfile);
									byte[] buffer = new byte[2048];
									int a=0;
									while ((a=is.read(buffer)) != -1) {
										os.write(buffer, 0, a);
									}
//									System.out.println("cfile.getName()"+cfile.getName());
									is.close();
									os.flush();
									os.close();
									Bundle bundle=new Bundle();
									Message message=Message.obtain();
									message.what=3;
									bundle.putString("msg", cfile.getAbsolutePath());
									message.setData(bundle);
									handler.sendMessage(message);
								}
							}
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					};
				}.start();
			}
		});
	}

	private void installAPK(String fileName){
		File file =new File(fileName);
		if(!file.exists()){
			return;
		}
		Intent intent=new Intent();
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.parse("file://"+file.toString()), "application/vnd.android.package-archive");
		startActivity(intent);
	}
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				Toast.makeText(getApplicationContext(), "网络不通",
						Toast.LENGTH_SHORT).show();
				break;
			case 2:
				 Toast.makeText(getApplicationContext(), "没有SD卡",
				 Toast.LENGTH_SHORT).show();
				 
				break;
			case 3:
				Bundle bundle = msg.getData();
				String fileName = bundle.getString("msg");
				installAPK(fileName);
				
				break;

			default:
				break;
			}
		};
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="43dp"
        android:layout_marginTop="19dp"
        android:text="Button" />

</RelativeLayout>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值