package com.ysh.wpc.appupdate.service; import android.annotation.SuppressLint; import android.app.Notification; import android.app.NotificationManager; import android.app.Service; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.widget.RemoteViews; import android.widget.Toast; import com.ysh.wpc.appupdate.R; import com.ysh.wpc.appupdate.download.DownLoadApks; import com.ysh.wpc.appupdate.download.DownLoadApks.IProgress; public class DownLoadFileService extends Service { private NotificationManager manager; private Notification notif; public static int j = -1; private String downloadUrl = "";// apk涓嬭浇璺緞 private String apkType; @Override public IBinder onBind(Intent intent) { return null; } @SuppressLint("NewApi") @Override public int onStartCommand(Intent intent, int flags, int startId) { Bundle bundle = (Bundle) intent.getExtras(); downloadUrl = bundle.getString("downloadurl", ""); apkType = ""; new DownLoadApks(this, new IProgress() { @Override public void getProgress(int p) { // stub if (p == 100) { handler.sendEmptyMessage(1); } else { if (manager == null || notif == null) { Toast.makeText(DownLoadFileService.this, "插件开始下载······", Toast.LENGTH_SHORT).show(); manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notif = new Notification(); // notif.largeIcon // ReadAssetsImage image = new ReadAssetsImage(); // notif.largeIcon = image.getImageFromAssetsFile( // DownLoadFileService.this, "ic_launcher"); notif.tickerText = "正在更新中"; notif.icon = R.drawable.ic_launcher; // 通知栏显示所用到的布局文件 notif.contentView = new RemoteViews(getPackageName(), R.layout.notify_item); } Message msg = handler.obtainMessage(); if (j != p) { msg.what = 0; msg.arg1 = p; handler.sendMessage(msg); } j = p; } } }, apkType).execute(downloadUrl); return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { super.onCreate(); } /** * 下载进度推送 */ private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: notif.contentView.setTextViewText(R.id.content_view_per, msg.arg1 + "%"); manager.notify(0, notif); break; case 1: notif.contentView .setTextViewText(R.id.content_view_per, "下载完成"); manager.notify(0, notif); j = 100; manager.cancelAll(); stopSelf(); break; default: break; } } }; @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); } @Override public void onDestroy() { super.onDestroy(); } }