Newer
Older
dxcgt / app / src / main / java / com / smartdot / cgt / util / PollingService.java
wangxitong on 6 Apr 2021 6 KB first commit
package com.smartdot.cgt.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Locale;

import org.apache.http.util.EncodingUtils;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Environment;
import android.os.IBinder;

import com.smartdot.cgt.request.Request;

/**
 * Polling service
 * @Author Ryan
 * @Create 2013-7-13 上午10:18:44
 */
public class PollingService extends Service {

	public static final String ACTION = "com.smartdot.cgt.util.PollingService";
	//final String userId = ApplicationMain.getInstance().getUserModel().getBgAdminId();
	final static String phoneNumber = null;
//	private Notification notification;
//	private NotificationManager mManager;
//	private WakeLock wakeLock = null;
	private PollingThread pollingThread;
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
//		acquireWakeLock()  ;
	}
	
//	private void acquireWakeLock()  
//    {  
//        if (null == wakeLock)  
//        {  
//            PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);  
//            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PostLocationService");  
//            if (null != wakeLock)  
//            {  
//                wakeLock.acquire();  
//            }  
//        }  
//    } 
   
   //释放设备电源锁  
//    private void releaseWakeLock()  
//    {  
//        if (null != wakeLock)  
//        {  
//            wakeLock.release();  
//            wakeLock = null;  
//        }  
//    } 
	
	@Override
	public void onStart(Intent intent, int startId) {
		pollingThread=new PollingThread(PollingService.this);
		pollingThread.start();
	}
	
	@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
	
        flags=START_STICKY;
        return super.onStartCommand(intent, flags, startId);
    }

//	private void initNotifiManager() {
//		mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//		int icon = R.drawable.ic_launcher;
//		mNotification = new Notification();
//		mNotification.icon = icon;
//		mNotification.tickerText = "New Message";
//		mNotification.defaults |= Notification.DEFAULT_SOUND;
//		mNotification.flags = Notification.FLAG_AUTO_CANCEL;
//	}

//	private void showNotification() {
//		notification = new Notification(R.drawable.ic_launcher,
//				 getString(R.string.app_name), System.currentTimeMillis());
//				
//				 PendingIntent pendingintent = PendingIntent.getActivity(this, 0,
//				 new Intent(this, FrmMain.class), 0);
//				 notification.setLatestEventInfo(this, "uploadservice", "请保持程序在后台运行",
//				 pendingintent);
//				 startForeground(0x111, notification);
//	}

	/**
	 * Polling thread
	 * @Author Ryan
	 * @Create 2013-7-13 上午10:18:34
	 */
//	int count = 0;
	private static class PollingThread extends Thread {
		private final PollingService pollingService;

	    private PollingThread(Context context) {
	        this.pollingService = (PollingService)context;
	    }
	    
		@Override
		public void run() {
			
//			showNotification();
		    String timeStr = null;
			Location location=	GpsManager.getGpsSingerton().getLocation();
			String longitude="0";
			String latitude="0" ;
			if(location!=null)
			{
				 Double b0=location.getLongitude();
				double b3=0;
				if(b0!=null&&!"".equals(b0))
				{
//	              BigDecimal bd1 = new BigDecimal(Double.toString(b0));
//	       	      BigDecimal bd2 = new BigDecimal("0.00127");
					b3= b0;
					// b3= bd1.subtract(bd2).doubleValue();
//	        		if(b3<0)
//	        		{
//	        			b3=0;
//	        		}
				}
				longitude= String.valueOf(b3);
				   latitude= Double.toString(location.getLatitude());	
			
			try {
				 
				 
				 StringBuffer sb = new StringBuffer(256);
					sb.append("longitude : "+longitude);
					sb.append("latitude : "+latitude);
					File configFolder = new File(Environment.getExternalStorageDirectory().getPath() + "/cgt/");
					if (!configFolder.exists())
					{
						configFolder.mkdirs();
					}
					File errLog = new File(configFolder, "GPS.log");
					try
					{
						if (!errLog.exists())
						{
							errLog.createNewFile();
						}
						OutputStream os = new FileOutputStream(errLog, true);
						SimpleDateFormat simpleDateFormat = new SimpleDateFormat("{MM-dd HH:mm:ss.SSS}", Locale.CHINA);
						timeStr = simpleDateFormat.format(System.currentTimeMillis());
						os.write(EncodingUtils.getBytes(timeStr + sb.toString()+"\n", "UTF-8"));
						os.flush();
						os.close();
					}
					catch (IOException e)
					{
						e.printStackTrace();
					}
					
					String userId="";
					if(ApplicationMain.getInstance()!=null&&ApplicationMain.getInstance().getUserModel()!=null&&ApplicationMain.getInstance().getUserModel().getBgAdminId()!=null)
					{
						userId=ApplicationMain.getInstance().getUserModel().getBgAdminId();
					}else{
						LoginVo vo=	PerformanceUtil.getUser(pollingService);	
						userId=vo.getBgadminid();
					}
				Request.getRequest().reportGpsPosition(phoneNumber, longitude, latitude, userId, 1);
			} catch (Exception e) {
				File configFolder = new File(Environment.getExternalStorageDirectory().getPath() + "/cgt/");
				if (!configFolder.exists())
				{
					configFolder.mkdirs();
				}
				File errLog = new File(configFolder, "PollingService.log");
				 StringBuffer sb = new StringBuffer(256);
				try
				{
					if (!errLog.exists())
					{
						errLog.createNewFile();
					}
					OutputStream os = new FileOutputStream(errLog, true);
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("{MM-dd HH:mm:ss.SSS}", Locale.CHINA);
					 timeStr = simpleDateFormat.format(System.currentTimeMillis());
					sb.append("------PollingService------"+e.getMessage());
					os.write(EncodingUtils.getBytes(timeStr + sb.toString()+"\n", "UTF-8"));
					os.flush();
					os.close();
				}
				catch (IOException e1)
				{
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
		  }
		}
	}
	
	@Override
	public void onDestroy() {
//		releaseWakeLock();
		try {
			
			pollingThread.interrupt();
			pollingThread.join(); 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.onDestroy();
		
	}
	
	

}