Android 升級下載 它們的定義Updates 相容版本

來源:互聯網
上載者:User

標籤:

Android 更新模組 它們的定義Update


寫這個總結是由於在項目中碰到了Android系統相容的BUG  
Android項目原本使用的是API提供的下載方法  例如以下:
DownloadManager downloadManager = (DownloadManager) getSystemService(Activity.DOWNLOAD_SERVICE);DownloadManager.Request request = new Request(Uri.parse(dl));request.setTitle(getString(R.string.app_name));request.setDestinationUri(getDownloadName(getIntent().getStringExtra("version_name")));long reference = downloadManager.enqueue(request);UserSettingHelper.getInstance().setUpgradeKey(reference);


但如三星  華為 等手機它內建的系統中 刪除掉了google服務從而導致崩潰 

在網上找了資料 然後改吧改吧總算高速修複了bug:
1) 主activity 
package com.example.updataapk;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;/** *  * @author baozi *  */public class MainActivity extends Activity {// 地址private String dl = "http://17shihui.cn/download/shihui.apk";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Function_Utility.setAppContext(getApplicationContext());findViewById(R.id.button1).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Uri uri = Uri.parse(dl);Intent intent = new Intent(Intent.ACTION_VIEW, uri);startActivity(intent);}});findViewById(R.id.button2).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {download(dl);} catch (Exception e) {e.printStackTrace();}}});}private void download(String dl) throws Exception {if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {Intent service = new Intent(this, DownloadService.class);service.putExtra(DownloadService.INTENT_URL, dl);startService(service);} else {Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(dl));startActivity(intent);}}}


2) 下載模組
package com.example.updataapk;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.os.Handler;import android.os.IBinder;import android.os.Message;import android.util.Log;import android.widget.RemoteViews;public class DownloadService extends Service {// notification 名字private String notify_name = "apk正在下載...";public static final String INTENT_URL = "url";private Context mContext = this;Notification mNotification;private static final int NOTIFY_ID = 0;private NotificationManager mNotificationManager;/* 下載包安裝路徑 */private static final String savePath = Function_Utility.getUpgradePath();private static final String saveFileName = savePath + "demo.apk";private String apkUrl;private int progress;boolean canceled;private Thread downLoadThread;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);}public int onStartCommand(Intent intent, int flags, int startId) {Log.i("DownloadService", "intent=" + intent.toString() + " ;           flags= " + flags + " ;    startId" + startId);if (intent.hasExtra(DownloadService.INTENT_URL)) {apkUrl = (String) intent.getExtras().get(DownloadService.INTENT_URL);}progress = 0;setUpNotification();new Thread() {public void run() {// 開始下載startDownload();};}.start();return startId;};private void startDownload() {canceled = false;downloadApk();}private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what) {case 0:// 完成下載// 取消通知mNotificationManager.cancel(NOTIFY_ID);installApk();break;case 2:// 這裡是使用者介面手動取消,所以會經過activity的onDestroy();方法// 取消通知mNotificationManager.cancel(NOTIFY_ID);break;case 1:int rate = msg.arg1;if (rate < 100) {RemoteViews contentview = mNotification.contentView;contentview.setTextViewText(R.id.tv_progress, rate + "%");contentview.setProgressBar(R.id.progressbar, 100, rate, false);} else {// 完成下載後變換通知形式mNotification.flags = Notification.FLAG_AUTO_CANCEL;mNotification.contentView = null;mNotification.setLatestEventInfo(mContext, "完成下載", "檔案已完成下載", null);stopSelf();// 停掉服務自身}PendingIntent contentIntent2 = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);mNotification.contentIntent = contentIntent2;mNotificationManager.notify(NOTIFY_ID, mNotification);break;case 3:mNotification.flags = Notification.FLAG_AUTO_CANCEL;RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.update_download_notification_layout);contentView.setTextViewText(R.id.name, "下載失敗");// 指定個人化視圖mNotification.contentView = contentView;Intent intent = new Intent(getApplicationContext(), MainActivity.class);PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);// 指定內容意圖mNotification.contentIntent = contentIntent;mNotificationManager.notify(NOTIFY_ID, mNotification);stopSelf();// 停掉服務自身break;}}};/** * 安裝apk *  * @param url */private void installApk() {File apkfile = new File(saveFileName);if (!apkfile.exists()) {return;}Intent i = new Intent(Intent.ACTION_VIEW);i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");mContext.startActivity(i);}private int lastRate = 0;private InputStream is = null;private FileOutputStream fos = null;/** * 下載apk *  * @param url */private void downloadApk() {downLoadThread = new Thread(mdownApkRunnable);downLoadThread.start();}private Runnable mdownApkRunnable = new Runnable() {@Overridepublic void run() {try {URL url = new URL(apkUrl);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.connect();int length = conn.getContentLength();is = conn.getInputStream();File file = new File(savePath);if (!file.exists()) {file.mkdirs();}String apkFile = saveFileName;File ApkFile = new File(apkFile);fos = new FileOutputStream(ApkFile);int count = 0;byte buf[] = new byte[1024];do {int numread = is.read(buf);count += numread;progress = (int) (((float) count / length) * 100);// 更新進度Message msg = mHandler.obtainMessage();msg.what = 1;msg.arg1 = progress;if (progress >= lastRate + 1) {mHandler.sendMessage(msg);lastRate = progress;}if (numread <= 0) {mHandler.sendEmptyMessage(0);// 完成下載通知安裝// 下載完了,cancelled也要設定canceled = true;break;}fos.write(buf, 0, numread);} while (!canceled);// 點擊取消就停止下載.Log.i("DownloadService----------canceled", canceled + "");fos.close();is.close();} catch (Exception e) {Message msg = mHandler.obtainMessage();msg.what = 3;mHandler.sendMessage(msg);e.printStackTrace();} finally {try {if (fos != null) {fos.close();}is.close();if (is != null) {is.close();}} catch (Exception e) {e.printStackTrace();}}}};/** * 建立通知 */private void setUpNotification() {int icon = R.drawable.ic_launcher;CharSequence tickerText = "開始下載";long when = System.currentTimeMillis();mNotification = new Notification(icon, tickerText, when);;// 放置在"正在執行"欄目中mNotification.flags = Notification.FLAG_ONGOING_EVENT;RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.update_download_notification_layout);contentView.setTextViewText(R.id.name, notify_name);// 指定個人化視圖mNotification.contentView = contentView;PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);// 指定內容意圖mNotification.contentIntent = contentIntent;mNotificationManager.notify(NOTIFY_ID, mNotification);}}

3) 下載到手機的地址 
package com.example.updataapk;import java.io.File;import java.io.IOException;import android.annotation.SuppressLint;import android.content.Context;import android.os.Environment;@SuppressWarnings("deprecation")@SuppressLint({ "DefaultLocale", "SimpleDateFormat" })public class Function_Utility {private static Context mAppContext;public static void setAppContext(Context context) {mAppContext = context;}public static Context getAppContext() {return mAppContext;}/** * 下載到SD卡地址 */public static String getUpgradePath() {String filePath = getAppRootPath() + "/upgrade/";File file = new File(filePath);if (!file.isDirectory()) {file.mkdirs();}file = null;return filePath;}public static String getAppRootPath() {String filePath = "/weimicommunity";if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {filePath = Environment.getExternalStorageDirectory() + filePath;} else {filePath = getAppContext().getCacheDir() + filePath;}File file = new File(filePath);if (!file.exists()) {file.mkdirs();}file = null;File nomedia = new File(filePath + "/.nomedia");if (!nomedia.exists())try {nomedia.createNewFile();} catch (IOException e) {e.printStackTrace();}return filePath;}}

4) Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.updataapk"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.updataapk.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>        <service android:name="com.example.updataapk.DownloadService" >        </service>    </application></manifest>

:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWFhd3FxcQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >

下載完畢後 自己主動安裝Demo :
http://download.csdn.net/detail/aaawqqq/8040081

著作權聲明:本文博主原創文章,部落格,未經同意不得轉載。

Android 升級下載 它們的定義Updates 相容版本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.