網路下載apk自動安裝小例子

來源:互聯網
上載者:User
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;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);btn = (Button) findViewById(R.id.button1);btn.setOnClickListener(new OnClickListener() {@Overridepublic 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 blocke.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;}};};@Overridepublic 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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.