网络下载apk自动安装小例子,网络apk例子,package feng
分享于 点击 35772 次 点评:69
网络下载apk自动安装小例子,网络apk例子,package feng
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>
用户点评