android利用service实现下载apk自动更新功能

使用时只需要修改apk下载保存的路径和保存名称,还有通知栏布局的图片,改为自己app的图标即可

service的实现如下

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.zznode.jsyw.R;
import com.zznode.jsyw.util.constant.Constants;

/**
 * @author Shuai8172
 * @date 2014年5月4日 下午2:14:33
 * @description
 * @version 1.0
 */
public class UpdateService extends Service {
	/** 服务的运行状态,0:未运行  1:正在运行 */
	public static int state = 0;
	/** 连接和读取的超时时间 */
	private static final int TIMEOUT = 10*1000;
	/** 下载中,更新通知栏 */
	private static final int DOWN_GOING = 0;
	/** 下载完成 */
	private static final int DOWN_OK = 1;
	/** 下载失败 */
	private static final int DOWN_ERROR = 2;
	/** 下载取消 */
	private static final int DOWN_CANCEL = 3;
	//下载路径
	private static final String SAVEPATH = "download";//使用时根据自己实际情况修改地址
	//下载保存文件名
	private static final String SAVENAME = "new.apk";<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">//使用时根据自己实际情况修改地址</span>
	private static final int notification_id=0;
	private NotificationManager notificationManager;
	private Notification notification;
	private PendingIntent pendingIntent;
	private String downUrl;
	private File saveFile;
	private Thread downThread;
	
	@Override
	public void onCreate() {
		super.onCreate();
		notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		state = 1;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		state = 0;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		String command = intent.getStringExtra("command");
		if("start".equals(command)){
			downUrl = intent.getStringExtra("downUrl");
			if(downUrl!=null && !downUrl.equals("")){
				if(!creatFile()){// 创建文件
					Toast.makeText(this, "创建文件失败,请检查SD卡是否正常", Toast.LENGTH_SHORT).show();
					stopSelf();
					return super.onStartCommand(intent, flags, startId);
				}
				createNotification();// 创建通知
				createThread();// 线程下载}
			}
		}else if("cancle".equals(command)){
			// 取消下载
			Message msg = handler.obtainMessage(DOWN_CANCEL);
			handler.sendMessage(msg);
		}
		return super.onStartCommand(intent, flags, startId);
	}

	private boolean creatFile() {
		if (android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment
				.getExternalStorageState())) {
			File rootFile = new File(Environment.getExternalStorageDirectory()
					+ File.separator + SAVEPATH);
			if (rootFile != null && !rootFile.exists()) {
				rootFile.mkdirs();
			}
			saveFile = new File(rootFile, SAVENAME);
			return true;
		}
		return false;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	/***
	 * 创建通知栏
	 */
	RemoteViews contentView;
	public void createNotification() {
		notification = new Notification();
		notification.icon = android.R.drawable.stat_sys_download;// 这个图标必须要设置,不然下面那个RemoteViews不起作用.
		// 这个参数是通知提示闪出来的值.
		notification.tickerText = "开始下载";
		notification.flags |= Notification.FLAG_ONGOING_EVENT|Notification.FLAG_NO_CLEAR;

		//使用自定的view来显示Notification
		contentView = new RemoteViews(getPackageName(),
				R.layout.notification_item);
//		contentView.setImageViewResource(R.id.notificationIcon, R.drawable.ic_launcher);
		contentView.setTextViewText(R.id.notificationTitle, "正在下载");
		contentView.setTextViewText(R.id.notificationPercent, "0%");
		contentView.setProgressBar(R.id.notificationProgress, 100, 0, false);
		contentView.setViewVisibility(R.id.btnCancel, View.VISIBLE);
		contentView.setOnClickPendingIntent(R.id.btnCancel, PendingIntent
			    .getService(this, 0, new Intent(this,UpdateService.class).putExtra("command",
			      "cancle"), 0));
		notification.contentView = contentView;
		notificationManager.notify(notification_id, notification);

	}

	/***
	 * 更新UI
	 */
	final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case DOWN_GOING:
				// 改变通知栏
				int updateCount = msg.arg1;
				contentView.setTextViewText(R.id.notificationPercent,
						updateCount + "%");
				contentView.setProgressBar(R.id.notificationProgress, 100,
						updateCount, false);
				// show_view
				notificationManager.notify(notification_id, notification);
				break;
			case DOWN_OK:
				// 下载完成,准备安装
				notification.tickerText = "下载完成,准备安装";
				notification.flags = Notification.FLAG_AUTO_CANCEL|Notification.FLAG_NO_CLEAR;
				contentView.setTextViewText(R.id.notificationTitle, "下载完成,点击安装");
				contentView.setViewVisibility(R.id.btnCancel, View.INVISIBLE);
				Uri uri = Uri.fromFile(saveFile);
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setDataAndType(uri,"application/vnd.android.package-archive");
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				pendingIntent = PendingIntent.getActivity(UpdateService.this, 0, intent, 0);
				notification.contentIntent = pendingIntent;
				notificationManager.notify(notification_id, notification);
				startActivity(intent);
				stopSelf();
				break;
			case DOWN_ERROR:
				notification.tickerText = "下载失败";
				contentView.setTextViewText(R.id.notificationTitle, "下载失败");
				notification.flags = Notification.FLAG_AUTO_CANCEL;
				Intent backintent = new Intent(UpdateService.this, ControllActivity.class);
				backintent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
				pendingIntent = PendingIntent.getActivity(UpdateService.this, 0, backintent, 0);
		
				notification.contentIntent = pendingIntent;
				notificationManager.notify(notification_id, notification);
				stopSelf();
				break;
			case DOWN_CANCEL:
				if(downThread != null && downThread.isAlive()){
					downThread.interrupt();
				}
				notificationManager.cancel(notification_id);
				stopSelf();
				break;
			default:
				stopSelf();
				break;
			}

		}

	};
	/**
	 * 开线程下载
	 */
	public void createThread() {
		

		final Message message = handler.obtainMessage();

		downThread = new Thread(new Runnable() {
			@Override
			public void run() {

				try {
					boolean result = downloadUpdateFile(downUrl,
							saveFile.getPath());
					if (result) {
						// 下载成功,准备安装
						message.what=DOWN_OK;
						handler.sendMessage(message);
					}

				} catch (IOException e) {
					message.what=DOWN_ERROR;
					handler.sendMessageDelayed(message,2000);
				}

			}
		});
		downThread.start();
	}
	/**
	 * 下载文件
	 * 
	 * @return
	 * @throws IOException 
	 */
	public boolean downloadUpdateFile(String downUrl, String file) throws IOException{
		int downStep = 1;// 提示step
		int totalSize = 0;// 文件总大小
		int downloadCount = 0;// 已经下载好的大小
		int updateCount = 0;// 已经下载的百分比
		InputStream inputStream;
		OutputStream outputStream;

		HttpURLConnection httpURLConnection;
		URL url = new URL(downUrl);
		httpURLConnection = (HttpURLConnection) url.openConnection();
		httpURLConnection.setConnectTimeout(TIMEOUT);
		httpURLConnection.setReadTimeout(TIMEOUT);
		// 获取下载文件的size
		totalSize = httpURLConnection.getContentLength();
		if (httpURLConnection.getResponseCode() == 404) {
			throw new IOException("文件不存在!");
		}
		inputStream = httpURLConnection.getInputStream();
		outputStream = new FileOutputStream(file, false);// 文件存在则覆盖掉
		try {
			byte buffer[] = new byte[1024];
			int readsize = 0;
			while ((readsize = inputStream.read(buffer)) != -1
					&& !Thread.currentThread().isInterrupted()) {
				outputStream.write(buffer, 0, readsize);
				downloadCount += readsize;// 时时获取下载到的大小
				// 根据downStep更新
				if (updateCount == 0
						|| (downloadCount * 100 / totalSize - downStep) >= updateCount) {
					updateCount += downStep;
					// 改变通知栏
					Message msg = handler.obtainMessage(DOWN_GOING);
					msg.arg1 = updateCount;
					handler.sendMessage(msg);
				}
			}
		} finally {
			if (httpURLConnection != null) {
				httpURLConnection.disconnect();
			}
			inputStream.close();
			outputStream.close();
		}
		return totalSize!=0&&totalSize==downloadCount;
	}

}
自定义的通知栏布局文件notification_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="3dp" >

    <ImageView
        android:id="@+id/notificationIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/sys_ico"
        android:layout_gravity="center_vertical"
        android:contentDescription="@string/edit_null" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="15dp"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/notificationImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/edit_null"
                android:src="@android:drawable/stat_sys_download" />

            <TextView
                android:id="@+id/notificationTitle"
                android:layout_toRightOf="@id/notificationImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/notificationPercent"
                android:layout_toRightOf="@id/notificationTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageButton 
                android:id="@+id/btnCancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:src="@android:drawable/ic_delete"
                android:background="@null"
                android:padding="0dp"
                android:contentDescription="@string/edit_null"/>
        </RelativeLayout>

        <ProgressBar
            android:id="@+id/notificationProgress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值