package com.weikou.beibeivideo.service;
|
|
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.weikou.beibeivideo.R;
|
import com.weikou.beibeivideo.entity.AdTag;
|
import com.weikou.beibeivideo.util.downutil.DownLoadApks;
|
import com.weikou.beibeivideo.util.downutil.DownLoadApks.IProgress;
|
|
import java.io.File;
|
|
import org.greenrobot.eventbus.EventBus;
|
|
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;
|
}
|
|
@Override
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
if (intent == null) {
|
return 0;
|
}
|
Bundle bundle = (Bundle) intent.getExtras();
|
downloadUrl = bundle.getString("downloadurl", "");
|
if (downloadUrl.contains("memezhibo")) {
|
apkType = "com.memezhibo.android";
|
} else {
|
apkType = "";
|
}
|
new DownLoadApks(this, new IProgress() {
|
|
@Override
|
public void getProgress(int p) {
|
// TODO Auto-generated method
|
// 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.icon = R.drawable.ic_launcher;
|
notif.tickerText = "插件下载";
|
// 通知栏显示所用到的布局文件
|
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) {
|
// TODO Auto-generated method stub
|
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;
|
File a = getExternalFilesDir("pptv");
|
if (a.listFiles().length == 2) {
|
AdTag tag=new AdTag();
|
EventBus.getDefault().post(tag);
|
}
|
manager.cancelAll();
|
stopSelf();
|
break;
|
default:
|
break;
|
}
|
}
|
|
};
|
|
@Override
|
public void onStart(Intent intent, int startId) {
|
// TODO Auto-generated method stub
|
super.onStart(intent, startId);
|
}
|
|
@Override
|
public void onDestroy() {
|
// TODO Auto-generated method stub
|
super.onDestroy();
|
}
|
|
}
|