package com.jiuqi.njt.service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.data.WorkTaskBeanLocal;
import com.jiuqi.njt.db.WorkTaskNewsBeanDBHelper;
import com.jiuqi.njt.inf.IUpLoad;
import com.jiuqi.njt.inf.ServiceInf;
import com.jiuqi.njt.inf.TaskInf;
import com.jiuqi.njt.service.impl.WorkTaskNewsBeanImp;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
/**
* @author Administrator
* 数据传输的service
*/
@SuppressLint("NewApi")
public class UploadRecordSerice extends Service implements TaskInf {
private WorkTaskNewsBeanDBHelper db;
List<WorkTaskBeanLocal> list = new ArrayList<WorkTaskBeanLocal>();
private WorkTaskBeanLocal bu;
private IBinder bind = new MyBind();
private ServiceInf inf;
private SimpleDateFormat sdf;
@Override
public IBinder onBind(Intent intent) {//当客户端调用bindService()方法时调用此函数
Log.wtf("SErvice", "2-2onBind");
return bind;
}
public void setListener(ServiceInf inf) {
this.inf = inf;
}
@Override
public void onCreate() {
Log.wtf("SErvice", "2-1onCreate");
super.onCreate();
// db = new WorkTaskNewsBeanDBHelper(getApplicationContext());
// upLoadForDB();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
db = new WorkTaskNewsBeanDBHelper(getApplicationContext());
upLoadForDB();
return super.onStartCommand(intent, flags, startId);
}
private void upLoadForDB() {
list.clear();
// if (db.queryAll() != null && db.queryAll().size() > 0) {
if (db.queryAll() != null && db.queryForState(1).size() > 0) {
if(db.queryForState(1).size()>0){
bu = db.queryForState(1).get(0);
upLoad(bu);
}
// else{
// return;
// }
} else {
stopService(new Intent(UploadRecordSerice.this,
UploadRecordSerice.class));
Log.wtf("停止服务stopService", "stopService");
}
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("停止服务onUnbind", "onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.wtf("停止服务onDestroy", "onDestroy");
}
public synchronized void upLoad(WorkTaskBeanLocal b) {
bu = b;
if (inf != null)
inf.currBean(b);
IUpLoad upload = new WorkTaskNewsBeanImp(getApplicationContext(),
getApplication());
if (upload != null) {
upload.setListener(this);
upload.upLoad();
}
}
@Override
public void onPreExecute() {
}
@Override
public void isSuccess(Object b) {
if ((Boolean) b) {
db.delect(bu);
if (inf != null)
inf.successBean(bu);
} else {
if (inf != null)
inf.failBean(bu);
}
upLoadForDB();
}
/**
* 显示通知栏
*/
// private void showNotification() {
// Log.e("xiao", "showNotification---------"+sdf.format(new Date()));
// if(list!=null&&list.size()>0){
// notif.number = list.size();
// notif.setLatestEventInfo(this, list.get(0).getName() + "正在上传", null,
// contentIntent);
// }
// if(list!=null&&list.size()==0){
// nm.cancel(0);
// }else{
// nm.notify(0, notif);
// }
// }
/**
* 该类是获得Service对象 //需用内部类继承Binder,并定义方法获取Service对象
* @author Administrator
*
*/
public class MyBind extends Binder {
public UploadRecordSerice getService() {
return UploadRecordSerice.this;
}
}
}