|
|
@@ -0,0 +1,645 @@
|
|
|
+package com.usai.redant.rautils.Service;
|
|
|
+
|
|
|
+import android.Manifest;
|
|
|
+import android.app.Notification;
|
|
|
+import android.app.NotificationManager;
|
|
|
+import android.app.PendingIntent;
|
|
|
+import android.app.Service;
|
|
|
+import android.content.BroadcastReceiver;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.content.IntentFilter;
|
|
|
+import android.content.SharedPreferences;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.database.sqlite.SQLiteDatabase;
|
|
|
+import android.location.Location;
|
|
|
+import android.location.LocationListener;
|
|
|
+import android.location.LocationManager;
|
|
|
+import android.net.ConnectivityManager;
|
|
|
+import android.net.NetworkInfo;
|
|
|
+import android.os.AsyncTask;
|
|
|
+import android.os.Binder;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.IBinder;
|
|
|
+import android.os.SystemClock;
|
|
|
+import android.support.v4.app.ActivityCompat;
|
|
|
+import android.util.Log;
|
|
|
+
|
|
|
+import com.usai.redant.rautils.Upload.RAUploadManager;
|
|
|
+import com.usai.redant.rautils.Utils.Network;
|
|
|
+import com.usai.redant.rautils.Utils.dbgUtil;
|
|
|
+
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+
|
|
|
+public abstract class RAService extends Service {
|
|
|
+
|
|
|
+ public static final int DEFAULT_TIMEINTERVAL = 60*1000;
|
|
|
+ public static final int DEFAULT_DISTANCE = 60*50;
|
|
|
+ public static final int FLAG_SERVICE_NONE = 0;
|
|
|
+ public static final int FLAG_SERVICE_LOCATION = 1 << 1;
|
|
|
+ public static final int FLAG_SERVICE_NOTIFICATION = 1 << 2;
|
|
|
+ public static final int FLAG_SERVICE_UPLOAD = 1 << 3;
|
|
|
+// public static final int FLAG_LOCATION_SERVICE = 1<<1;
|
|
|
+
|
|
|
+ protected int service_flag = FLAG_SERVICE_NONE;
|
|
|
+ private IntentFilter msgFilter = new IntentFilter();
|
|
|
+
|
|
|
+ private RAUploadManager uploadManager = null;
|
|
|
+
|
|
|
+
|
|
|
+ LocationListener locationListener = null;
|
|
|
+ LocationManager locationManager = null;
|
|
|
+ int locationTracing_timeInterval = DEFAULT_TIMEINTERVAL ;
|
|
|
+ int locationTracing_distance = DEFAULT_DISTANCE ;
|
|
|
+
|
|
|
+ private checkPushTask m_task = null;
|
|
|
+
|
|
|
+ class checkPushTask extends AsyncTask<Void, Void, Boolean>
|
|
|
+ {
|
|
|
+ int errorcode;
|
|
|
+ String content = null;
|
|
|
+ Context mcontext;
|
|
|
+
|
|
|
+ public checkPushTask(Context context)
|
|
|
+ {
|
|
|
+ mcontext = context;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected Boolean doInBackground(Void... params)
|
|
|
+ {
|
|
|
+ Log.d("SearchTask", "doInBackground");
|
|
|
+ ;
|
|
|
+ if (!Network.isNetworkIsAvailable(getApplication()))
|
|
|
+
|
|
|
+ {
|
|
|
+ errorcode = Network.RESULT_NET_NOTAVAILABLE;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String jstr = "";
|
|
|
+ jstr = Network.check_push(mcontext,new Bundle());
|
|
|
+ // if (module_name.equals("Announcements"))
|
|
|
+ // jstr = Network.get_announcements(lastid, limit);
|
|
|
+ // else
|
|
|
+ // jstr = Network.get_marketnews(lastid, limit);
|
|
|
+ if (jstr == null || jstr.length() <= 0)
|
|
|
+ {
|
|
|
+ // Log.d(TAG, "json is wrong");
|
|
|
+
|
|
|
+ errorcode = Network.RESULT_NET_ERROR;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ content = jstr;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onPostExecute(Boolean success)
|
|
|
+ {
|
|
|
+ String tag = "onPostExec";
|
|
|
+ Log.i( tag, "entry");
|
|
|
+ m_task = null;
|
|
|
+ // showProgress(false);
|
|
|
+
|
|
|
+ // switch (errorcode)
|
|
|
+ // {
|
|
|
+ // // case Network.RESULT_NET_NOTAVAILABLE:
|
|
|
+ // // {
|
|
|
+ // // Toast toast = Toast.makeText(
|
|
|
+ // // ApexTrackingApplication.get_instance(),
|
|
|
+ // // getText(R.string.msg_connection_none),
|
|
|
+ // // Toast.LENGTH_LONG);
|
|
|
+ // // toast.setGravity(Gravity.CENTER, 0, 0);
|
|
|
+ // // toast.show();
|
|
|
+ // // break;
|
|
|
+ // // }
|
|
|
+ // // case Network.RESULT_NET_ERROR:
|
|
|
+ // // {
|
|
|
+ // // Toast toast = Toast.makeText(
|
|
|
+ // // ApexTrackingApplication.get_instance(),
|
|
|
+ // // getText(R.string.msg_net_error), Toast.LENGTH_LONG);
|
|
|
+ // // toast.setGravity(Gravity.CENTER, 0, 0);
|
|
|
+ // // toast.show();
|
|
|
+ // // break;
|
|
|
+ // // }
|
|
|
+ // // case Network.RESULT_ERROR:
|
|
|
+ // // // case Network.RESULT_RESPONSE_NULL:
|
|
|
+ // // {
|
|
|
+ // // Toast toast = Toast.makeText(
|
|
|
+ // // ApexTrackingApplication.get_instance(),
|
|
|
+ // // getText(R.string.msg_net_resulterror),
|
|
|
+ // // Toast.LENGTH_LONG);
|
|
|
+ // // toast.setGravity(Gravity.CENTER, 0, 0);
|
|
|
+ // // toast.show();
|
|
|
+ // // break;
|
|
|
+ // // }
|
|
|
+ //
|
|
|
+ // default:
|
|
|
+ // break;
|
|
|
+ // }
|
|
|
+
|
|
|
+ if (success)
|
|
|
+ {
|
|
|
+// JSONObject jsobj;
|
|
|
+// //
|
|
|
+// // array = new JSONArray(json);
|
|
|
+// try
|
|
|
+// {
|
|
|
+// jsobj = new JSONObject(content);
|
|
|
+//
|
|
|
+// String message = jsobj.getString("message");
|
|
|
+// String date = jsobj.getString("date");
|
|
|
+// String s_id = jsobj.getString("s_id");
|
|
|
+// String e_id = jsobj.getString("e_id");
|
|
|
+// int count = jsobj.getInt("count");
|
|
|
+// SQLiteDatabase db = dbUtil.OpenDB(
|
|
|
+// ApexTrackingApplication.get_instance(), null, true);
|
|
|
+// db.execSQL("insert into push_message(s_id,e_id,msgcount,message,h_time,user,create_time,read) values('"
|
|
|
+// + s_id
|
|
|
+// + "','"
|
|
|
+// + e_id
|
|
|
+// + "',"
|
|
|
+// + count
|
|
|
+// + ",'"
|
|
|
+// + message
|
|
|
+// + "','"
|
|
|
+// + date
|
|
|
+// + "','"
|
|
|
+// + ApexTrackingApplication.get_user()
|
|
|
+// + "',"
|
|
|
+// + System.currentTimeMillis() + ",0)");
|
|
|
+// dbUtil.CloseDB(db);
|
|
|
+//
|
|
|
+// boolean bnotify = mcontext.getSharedPreferences("setting",
|
|
|
+// 0).getBoolean("notifications_new_message", true);
|
|
|
+// if (bnotify)
|
|
|
+// {
|
|
|
+//
|
|
|
+// boolean bsound = mcontext.getSharedPreferences(
|
|
|
+// "setting", 0).getBoolean(
|
|
|
+// "notifications_new_message_sound", true);
|
|
|
+// boolean bvibrate = mcontext.getSharedPreferences(
|
|
|
+// "setting", 0).getBoolean(
|
|
|
+// "notifications_new_message_vibrate", true);
|
|
|
+// NotificationManager nManager = (NotificationManager) mcontext
|
|
|
+// .getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
+//
|
|
|
+//// Notification notification = new Notification(
|
|
|
+//// R.drawable.ic_launcher,
|
|
|
+//// mcontext.getString(R.string.str_notification_title),
|
|
|
+//// System.currentTimeMillis());
|
|
|
+// Intent intent = new Intent(mcontext,
|
|
|
+// FunctionSelectActivity.class);
|
|
|
+// intent.putExtra("launcher", "notification");
|
|
|
+//
|
|
|
+// PendingIntent pintent = PendingIntent.getActivity(
|
|
|
+// mcontext, 0, intent,
|
|
|
+// PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
+//
|
|
|
+//
|
|
|
+// Notification.Builder builder = new Notification.Builder(mcontext);//新建Notification.Builder对象
|
|
|
+// String ntitle = "";
|
|
|
+// int number = ApexTrackingApplication.ncount++;
|
|
|
+// if (number > 1)
|
|
|
+// ntitle=number
|
|
|
+// + " "
|
|
|
+// + ApexTrackingApplication
|
|
|
+// .get_instance()
|
|
|
+// .getString(
|
|
|
+// R.string.str_mnotification_title);
|
|
|
+// else
|
|
|
+// ntitle = ApexTrackingApplication
|
|
|
+// .get_instance()
|
|
|
+// .getString(
|
|
|
+// R.string.str_notification_title);
|
|
|
+//
|
|
|
+// builder.setContentTitle(ntitle);//设置标题
|
|
|
+// builder.setContentText(ApexTrackingApplication
|
|
|
+// .get_instance()
|
|
|
+// .getString(
|
|
|
+// R.string.str_notification_text)
|
|
|
+// + date);//设置内容
|
|
|
+// builder.setSmallIcon(R.drawable.ic_launcher);//设置图片
|
|
|
+// builder.setContentIntent(pintent);//执行intent
|
|
|
+// Notification notification = builder.getNotification();//将builder对象转换为普通的notifications
|
|
|
+//
|
|
|
+// if (bsound && bvibrate)
|
|
|
+// notification.defaults = Notification.DEFAULT_ALL;
|
|
|
+// else if (bsound)
|
|
|
+// notification.defaults = Notification.DEFAULT_SOUND
|
|
|
+// | Notification.DEFAULT_LIGHTS;
|
|
|
+// else if (bvibrate)
|
|
|
+// notification.defaults = Notification.DEFAULT_VIBRATE
|
|
|
+// | Notification.DEFAULT_LIGHTS;
|
|
|
+// notification.flags = Notification.FLAG_AUTO_CANCEL;
|
|
|
+// notification.number = number;
|
|
|
+//
|
|
|
+// nManager.notify(R.layout.activity_apex, notification);
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+// }
|
|
|
+// catch (JSONException e)
|
|
|
+// {
|
|
|
+// // TODO Auto-generated catch block
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ super.onPostExecute(success);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCancelled()
|
|
|
+ {
|
|
|
+ m_task = null;
|
|
|
+ // showProgress(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public void checkpush(Context context)
|
|
|
+ {
|
|
|
+
|
|
|
+ if (m_task != null)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // mStatusMessageView.setText(R.string.str_Loading);
|
|
|
+ // showProgress(true);
|
|
|
+ m_task = new checkPushTask(context);
|
|
|
+
|
|
|
+ // TextView text_page = (TextView) view_page_footer
|
|
|
+ // .findViewById(R.id.text_page);
|
|
|
+ // text_page.setText("Loading...");
|
|
|
+ // text_page.setEnabled(false);
|
|
|
+ m_task.execute();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public RAService() {
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected abstract void Setup();
|
|
|
+
|
|
|
+ protected abstract void onLocationChanged(Location location);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IBinder onBind(Intent intent) {
|
|
|
+// TODO: Return the communication channel to the service.
|
|
|
+// throw new UnsupportedOperationException("Not yet implemented");
|
|
|
+ Log.d("", "onBind: bind service");
|
|
|
+ return new MyBinder();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public class MyBinder extends Binder {
|
|
|
+ /**
|
|
|
+ * 获取当前Service的实例
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public RAService getService() {
|
|
|
+ return RAService.this;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCreate() {
|
|
|
+
|
|
|
+ Log.d("_SERVICE", "create upload Service!!!");
|
|
|
+
|
|
|
+
|
|
|
+// // 初始化异常捕获器
|
|
|
+// CrashHandler.getSharedInstance().init(getApplicationContext());
|
|
|
+// CrashHandler.getSharedInstance().preserver = new CrashHandler.InfoPreserver() {
|
|
|
+// @Override
|
|
|
+// public void saveUserInformation() {
|
|
|
+// if (uploadManager != null) {
|
|
|
+// uploadManager.saveTasks();
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void handleCrashInfo(String deviceInfo, String exception) {
|
|
|
+// dbgUtil.fileLog(getApplicationContext(),"============================ app crash ============================");
|
|
|
+// dbgUtil.fileLog(getApplicationContext(),"DEVICE");
|
|
|
+// dbgUtil.fileLog(getApplicationContext(),deviceInfo);
|
|
|
+// dbgUtil.fileLog(getApplicationContext(),"EXCEPTION");
|
|
|
+// dbgUtil.fileLog(getApplicationContext(),exception);
|
|
|
+// dbgUtil.fileLog(getApplicationContext(),"=============================== end ===============================");
|
|
|
+// Log.d("_SERVICE", "Device Info: " + deviceInfo);
|
|
|
+// Log.d("_SERVICE", "Crash Info: " + exception);
|
|
|
+// }
|
|
|
+// };
|
|
|
+
|
|
|
+
|
|
|
+ Setup();
|
|
|
+
|
|
|
+ msgFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
|
|
+
|
|
|
+ if ((service_flag & FLAG_SERVICE_UPLOAD) == 1) {
|
|
|
+ uploadManager = new RAUploadManager();
|
|
|
+// msgFilter.addAction("REDANT.POP.RESET_LOCATION");
|
|
|
+// msgFilter.addAction("REDANT.POP.QUERY_UPLOAD_STATE");
|
|
|
+ msgFilter.addAction("REDANT.BROADCAST.ACTION_UPLOAD_ADD_TASK");
|
|
|
+// msgFilter.addAction("REDANT.BROADCAST.UPLOAD_MODIFY_QUEUE");
|
|
|
+// msgFilter.addAction("REDANT.BROADCAST.UPLOAD_RETRY_UPLOAD");
|
|
|
+ }
|
|
|
+ if ((service_flag & FLAG_SERVICE_LOCATION) == 1) {
|
|
|
+
|
|
|
+ msgFilter.addAction("REDANT.BROADCAST.ACTION_LOCATION_ENABLE_TRACING");
|
|
|
+ msgFilter.addAction("REDANT.BROADCAST.ACTION_LOCATION_DISABLE_TRACING");
|
|
|
+ msgFilter.addAction("REDANT.BROADCAST.ACTION_LOCATION_REQUEST_LOCATION");
|
|
|
+// msgFilter.addAction("REDANT.BROADCAST.RESET_LOCATION");
|
|
|
+ locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
|
|
+
|
|
|
+ locationListener = new LocationListener() {
|
|
|
+
|
|
|
+ // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
|
|
|
+ @Override
|
|
|
+ public void onStatusChanged(String provider, int status,
|
|
|
+ Bundle extras) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // Provider被enable时触发此函数,比如GPS被打开
|
|
|
+ @Override
|
|
|
+ public void onProviderEnabled(String provider) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // Provider被disable时触发此函数,比如GPS被关闭
|
|
|
+ @Override
|
|
|
+ public void onProviderDisabled(String provider) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
|
|
|
+ @Override
|
|
|
+ public void onLocationChanged(Location location) {
|
|
|
+ onLocationChanged(location);
|
|
|
+
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ enable_locationTracing(locationTracing_timeInterval,locationTracing_distance);
|
|
|
+
|
|
|
+
|
|
|
+// msgFilter.addAction("REDANT.POP.GPS_ON");
|
|
|
+// msgFilter.addAction("REDANT.POP.GPS_OFF");
|
|
|
+// msgFilter.addAction("REDANT.POP.REQUEST_LOCATION");
|
|
|
+ }
|
|
|
+ if ((service_flag & FLAG_SERVICE_NOTIFICATION) == 1) {
|
|
|
+
|
|
|
+ msgFilter.addAction("REDANT.BROADCAST.ACTION_PUSHNOTIFICATION_CHECK");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ registerReceiver(uploadReceiver, msgFilter);
|
|
|
+
|
|
|
+ super.onCreate();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDestroy() {
|
|
|
+ Log.e("_SERVICE", "onDestroy: ");
|
|
|
+ if (uploadManager != null)
|
|
|
+ uploadManager.saveTasks();
|
|
|
+ }
|
|
|
+
|
|
|
+ private BroadcastReceiver uploadReceiver = new BroadcastReceiver() {
|
|
|
+
|
|
|
+ public void onReceive(Context context, Intent intent) {
|
|
|
+ String action = intent
|
|
|
+ .getAction();
|
|
|
+ // 如果捕捉到的action是ACTION_BATTERY_CHANGED
|
|
|
+
|
|
|
+ if ("REDANT.BROADCAST.ACTION_UPLOAD_ADD_TASK".equals(action)) {
|
|
|
+
|
|
|
+ ArrayList<Bundle> tasks = intent.getParcelableArrayListExtra("tasks");
|
|
|
+
|
|
|
+
|
|
|
+ uploadManager.addTasks(tasks);
|
|
|
+ }
|
|
|
+// else if ("REDANT.RAImage.SAVE_TASK".equals(action)) {
|
|
|
+// uploadManager.stopAllTasks();
|
|
|
+// }
|
|
|
+ else if (intent
|
|
|
+ .getAction()
|
|
|
+ .equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
|
|
|
+
|
|
|
+ ConnectivityManager connManager = (ConnectivityManager) context
|
|
|
+ .getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+
|
|
|
+ NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
|
|
|
+ if (networkInfo == null) {
|
|
|
+ dbgUtil.Logd(
|
|
|
+ "Current Network info",
|
|
|
+ "can not get Active NetworkInfo!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ NetworkInfo.State netState = networkInfo
|
|
|
+ .getState();
|
|
|
+ if (netState != NetworkInfo.State.CONNECTED) {
|
|
|
+ dbgUtil.Logd(
|
|
|
+ "Current Network info",
|
|
|
+ "not Connected!State="
|
|
|
+ + netState);
|
|
|
+ return;
|
|
|
+ } else {
|
|
|
+
|
|
|
+ int iconntype = -1;
|
|
|
+ iconntype = networkInfo
|
|
|
+ .getType();
|
|
|
+ SharedPreferences pref = getApplication()
|
|
|
+ .getSharedPreferences(
|
|
|
+ "UploadManager",
|
|
|
+ 0);
|
|
|
+
|
|
|
+
|
|
|
+ boolean
|
|
|
+ wifi_only
|
|
|
+ = pref.getBoolean("wifi_only",
|
|
|
+ false);
|
|
|
+
|
|
|
+ if
|
|
|
+ (wifi_only
|
|
|
+ == true
|
|
|
+ && iconntype !=
|
|
|
+ ConnectivityManager.TYPE_WIFI
|
|
|
+ && iconntype !=
|
|
|
+ 9/* earthnet */) {
|
|
|
+ uploadManager.stopAllTasks();
|
|
|
+// dbgUtil.Log(Log.DEBUG,
|
|
|
+// "Current Network info",
|
|
|
+// "not allowed!Connection type="
|
|
|
+// +
|
|
|
+// networkInfo.getTypeName());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ else if ("REDANT.BROADCAST.ACTION_LOCATION_ENABLE_TRACING"
|
|
|
+ .equals(action))
|
|
|
+ {
|
|
|
+ enable_locationTracing(locationTracing_timeInterval,locationTracing_distance);
|
|
|
+ }
|
|
|
+ else if ("REDANT.BROADCAST.ACTION_LOCATION_DISABLE_TRACING"
|
|
|
+ .equals(action))
|
|
|
+ {
|
|
|
+
|
|
|
+ disable_locationTracing();
|
|
|
+ }
|
|
|
+ else if ("REDANT.BROADCAST.ACTION_LOCATION_REQUEST_LOCATION"
|
|
|
+ .equals(action))
|
|
|
+ {
|
|
|
+ request_location(intent);
|
|
|
+ }
|
|
|
+ else if ("REDANT.BROADCAST.ACTION_PUSHNOTIFICATION_CHECK"
|
|
|
+ .equals(action))
|
|
|
+ {
|
|
|
+
|
|
|
+ String tag = "onReceive@Alarmreceiver";
|
|
|
+ Log.i( tag, "receive alarm broadcast caller =="
|
|
|
+ + intent.getStringExtra("caller"));
|
|
|
+
|
|
|
+// if (SystemClock.elapsedRealtime()
|
|
|
+// - ApexTrackingApplication.getLastCheckMessageTime() > 120000)
|
|
|
+// {
|
|
|
+// Log.i(tag, "check messsage");
|
|
|
+// ApexTrackingApplication.UpdateLastCheckMessageTime();
|
|
|
+// checkpush(context);
|
|
|
+//
|
|
|
+// }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ void registerReceiver() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ void disable_locationTracing() {
|
|
|
+ locationManager.removeUpdates(locationListener);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ void enable_locationTracing(int timeInterval, int distance ) {
|
|
|
+
|
|
|
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ // TODO: Consider calling
|
|
|
+ // ActivityCompat#requestPermissions
|
|
|
+ // here to request the missing permissions, and then overriding
|
|
|
+ // public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
+ // int[] grantResults)
|
|
|
+ // to handle the case where the user grants the permission. See the documentation
|
|
|
+ // for ActivityCompat#requestPermissions for more details.
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
|
|
+
|
|
|
+
|
|
|
+ locationManager.requestLocationUpdates(
|
|
|
+ LocationManager.GPS_PROVIDER, timeInterval, distance,
|
|
|
+ locationListener);
|
|
|
+
|
|
|
+ } else if (locationManager
|
|
|
+ .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
|
|
|
+
|
|
|
+
|
|
|
+ locationManager.requestLocationUpdates(
|
|
|
+ LocationManager.NETWORK_PROVIDER, timeInterval, distance,
|
|
|
+ locationListener);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void request_location(Intent intent)
|
|
|
+ {
|
|
|
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ // TODO: Consider calling
|
|
|
+ // ActivityCompat#requestPermissions
|
|
|
+ // here to request the missing permissions, and then overriding
|
|
|
+ // public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
+ // int[] grantResults)
|
|
|
+ // to handle the case where the user grants the permission. See the documentation
|
|
|
+ // for ActivityCompat#requestPermissions for more details.
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Location location = null;
|
|
|
+ if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
|
|
|
+ {
|
|
|
+
|
|
|
+ location = locationManager
|
|
|
+ .getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
|
|
+
|
|
|
+ }
|
|
|
+ else if (locationManager
|
|
|
+ .isProviderEnabled(LocationManager.NETWORK_PROVIDER))
|
|
|
+ {
|
|
|
+
|
|
|
+ location = locationManager
|
|
|
+ .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+// SharedPreferences pref = RedAntApplication.getInstance()
|
|
|
+// .getSharedPreferences("POP", 0);
|
|
|
+// double lat = pref.getFloat("Lat", 9999);
|
|
|
+// double lon = pref.getFloat("Lon", 9999);
|
|
|
+//
|
|
|
+// // SharedPreferences.Editor editor = pref.edit();
|
|
|
+// if (location != null)
|
|
|
+// {
|
|
|
+// float[] result = new float[1];
|
|
|
+// if (lat != 9999 && lon != 9999)
|
|
|
+// {
|
|
|
+// Location.distanceBetween(lat, lon, location.getLatitude(),
|
|
|
+// location.getLongitude(), result);
|
|
|
+// if (result[0] > 1000)
|
|
|
+// {
|
|
|
+// sendBroadcast(new Intent("REDANT.POP.STATION_CHANGE"));
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// else
|
|
|
+// {
|
|
|
+// sendBroadcast(new Intent("REDANT.POP.STATION_NOT_CHANGE"));
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// sendBroadcast(new Intent("REDANT.POP.STATION_NOT_CHANGE"));
|
|
|
+// return;
|
|
|
+// // new Location();
|
|
|
+// //
|
|
|
+// // Location.distanceBetween(lat, lon, endLatitude, endLongitude,
|
|
|
+// // results)
|
|
|
+// //
|
|
|
+// // editor.putFloat("Lat", (float) location.getLatitude());
|
|
|
+// // editor.putFloat("Lon", (float) location.getLongitude());
|
|
|
+//
|
|
|
+// }
|
|
|
+// else
|
|
|
+// {
|
|
|
+// sendBroadcast(new Intent("REDANT.POP.STATION_NOT_CHANGE"));
|
|
|
+// return;
|
|
|
+// }
|
|
|
+ // editor.commit();
|
|
|
+
|
|
|
+ // Log.d("location:", "lat:" + latitude + "lon:" + longitude);
|
|
|
+ }
|
|
|
+}
|