ApexTrackingApplication.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package com.usai.apex;
  2. import com.usai.util.Crypto;
  3. import com.usai.util.dbUtil;
  4. import android.app.AlarmManager;
  5. import android.app.Application;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.SharedPreferences;
  11. import android.os.SystemClock;
  12. import android.text.TextUtils;
  13. import android.util.Log;
  14. public class ApexTrackingApplication extends Application
  15. {
  16. static final String TAG = "ApexTrackingApplication";
  17. private static ApexTrackingApplication instance;
  18. private static String m_sessionid = "";
  19. private static String m_user = "";
  20. private static String m_password = "";
  21. public static int ncount = 1;
  22. private static boolean m_bauthorized = false;
  23. private static long LastAlermTime = -120000;
  24. public static void UpdateLastCheckMessageTime()
  25. {
  26. LastAlermTime = SystemClock.elapsedRealtime();
  27. }
  28. public static long getLastCheckMessageTime()
  29. {
  30. return LastAlermTime;
  31. }
  32. @Override
  33. public void onCreate()
  34. {
  35. // TODO Auto-generated method stub
  36. Log.d(TAG, "onCreate");
  37. super.onCreate();
  38. instance = this;
  39. SharedPreferences pref = ApexTrackingApplication.get_instance()
  40. .getSharedPreferences("Apex", 0);
  41. boolean autologin = pref.getBoolean("autologin", false);
  42. if (autologin == true)
  43. {
  44. String u = pref.getString("user", "");
  45. String p = pref.getString("password", "");
  46. String s = pref.getString("sessionid", "");
  47. try
  48. {
  49. if (!TextUtils.isEmpty(u))
  50. m_user = Crypto.decrypt("apexu", u);
  51. if (!TextUtils.isEmpty(p))
  52. m_password = Crypto.decrypt("apexp", p);
  53. if (!TextUtils.isEmpty(s))
  54. m_sessionid = Crypto.decrypt("apexp", s);
  55. }
  56. catch (Exception e)
  57. {
  58. // TODO Auto-generated catch block
  59. e.printStackTrace();
  60. }
  61. }
  62. dbUtil.cleanhistory();
  63. // Intent serviceintent = new Intent();
  64. // serviceintent.setClass(this, ApexPushService.class);
  65. //
  66. //
  67. // startService(serviceintent);
  68. }
  69. // @Override
  70. // protected void finalize() throws Throwable
  71. // {
  72. // NotificationManager nManager = (NotificationManager)
  73. // instance.getSystemService(
  74. // Context.NOTIFICATION_SERVICE);
  75. // nManager.cancel(R.layout.activity_apex);
  76. // super.finalize();
  77. // }
  78. public static ApexTrackingApplication get_instance()
  79. {
  80. return instance;
  81. }
  82. public static void startalarm(String caller)
  83. {
  84. SharedPreferences pref = get_instance()
  85. .getSharedPreferences("Apex", 0);
  86. boolean autologin = pref.getBoolean("autologin", false);
  87. if (autologin == false)
  88. return;
  89. // 启动完成
  90. Intent iAlarm = new Intent(get_instance(), Alarmreceiver.class);
  91. iAlarm.putExtra("caller", caller);
  92. iAlarm.setAction("com.usai.apex.push");
  93. PendingIntent sender = PendingIntent.getBroadcast(get_instance(), 0,
  94. iAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
  95. long firstime = SystemClock.elapsedRealtime();
  96. // UpdateLastAlermTime();
  97. AlarmManager am = (AlarmManager) get_instance().getSystemService(
  98. Context.ALARM_SERVICE);
  99. // 5分钟一个周期,不停的发送广播
  100. am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime,
  101. 300 * 1000, sender);
  102. }
  103. public static void cancelalarm()
  104. {
  105. // 启动完成
  106. Intent iAlarm = new Intent(get_instance(), Alarmreceiver.class);
  107. iAlarm.setAction("com.usai.apex.push");
  108. PendingIntent sender = PendingIntent.getBroadcast(get_instance(), 0,
  109. iAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
  110. AlarmManager am = (AlarmManager) get_instance().getSystemService(
  111. Context.ALARM_SERVICE);
  112. am.cancel(sender);
  113. }
  114. public static void logout()
  115. {
  116. ncount = 1;
  117. cancelalarm();
  118. m_sessionid = "";
  119. m_user = "";
  120. m_password = "";
  121. m_bauthorized = false;
  122. SharedPreferences pref = ApexTrackingApplication.get_instance()
  123. .getSharedPreferences("Apex", 0);
  124. SharedPreferences.Editor editor = pref.edit();
  125. editor.putBoolean("autologin", false);
  126. editor.commit();
  127. }
  128. public static void SetAuthorizeStatus(boolean bauthorized)
  129. {
  130. m_bauthorized = bauthorized;
  131. }
  132. public static void login(String sid, String user, String pass)
  133. {
  134. m_sessionid = sid;
  135. m_user = user;
  136. m_password = pass;
  137. m_bauthorized = true;
  138. SharedPreferences pref = ApexTrackingApplication.get_instance()
  139. .getSharedPreferences("Apex", 0);
  140. SharedPreferences.Editor editor = pref.edit();
  141. try
  142. {
  143. editor.putString("user", Crypto.encrypt("apexu", m_user));
  144. editor.putString("password", Crypto.encrypt("apexp", m_password));
  145. editor.putBoolean("autologin", true);
  146. editor.putString("sessionid", Crypto.encrypt("apexp", m_sessionid));
  147. }
  148. catch (Exception e)
  149. {
  150. editor.putString("user", null);
  151. editor.putString("password", null);
  152. editor.putString("m_sessionid", null);
  153. editor.putBoolean("autologin", false);
  154. m_bauthorized=false;
  155. e.printStackTrace();
  156. }
  157. editor.commit();
  158. startalarm("app Login");
  159. }
  160. // public static void put_sessionid(String id)
  161. // {
  162. //
  163. // Log.e(TAG, "put_sessionid");
  164. // m_sessionid = id;
  165. // }
  166. // public static void put_authorization(boolean bauth)
  167. // {
  168. //
  169. // m_bauthorized = bauth;
  170. // }
  171. public static boolean get_authorization()
  172. {
  173. return m_bauthorized;
  174. }
  175. // public static void put_user(String user)
  176. // {
  177. //
  178. // m_user = user;
  179. // }
  180. //
  181. // public static void put_password(String pass)
  182. // {
  183. //
  184. // m_password = pass;
  185. // }
  186. public static String get_sessionid()
  187. {
  188. Log.d(TAG, "get_sessionid");
  189. return m_sessionid;
  190. }
  191. public static String get_user()
  192. {
  193. Log.d(TAG, "get_user");
  194. return m_user;
  195. }
  196. public static String get_pass()
  197. {
  198. Log.d(TAG, "get_pass");
  199. return m_password;
  200. }
  201. }