Alarmreceiver.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package com.usai.apex;
  2. import org.json.JSONException;
  3. import org.json.JSONObject;
  4. import com.usai.util.Crypto;
  5. import com.usai.util.Network;
  6. import com.usai.util.dbUtil;
  7. import android.app.Notification;
  8. import android.app.NotificationManager;
  9. import android.app.PendingIntent;
  10. import android.content.BroadcastReceiver;
  11. import android.content.Context;
  12. import android.content.Intent;
  13. import android.content.SharedPreferences;
  14. import android.database.sqlite.SQLiteDatabase;
  15. import android.os.AsyncTask;
  16. import android.os.Parcelable;
  17. import android.os.SystemClock;
  18. import android.util.Log;
  19. public class Alarmreceiver extends BroadcastReceiver
  20. {
  21. private SearchTask m_task = null;
  22. public void checkpush(Context context)
  23. {
  24. if (m_task != null)
  25. {
  26. return;
  27. }
  28. // mStatusMessageView.setText(R.string.str_Loading);
  29. // showProgress(true);
  30. m_task = new SearchTask(context);
  31. // TextView text_page = (TextView) view_page_footer
  32. // .findViewById(R.id.text_page);
  33. // text_page.setText("Loading...");
  34. // text_page.setEnabled(false);
  35. m_task.execute();
  36. }
  37. class SearchTask extends AsyncTask<Void, Void, Boolean>
  38. {
  39. int errorcode;
  40. String content = null;
  41. Context mcontext;
  42. public SearchTask(Context context)
  43. {
  44. mcontext = context;
  45. }
  46. @Override
  47. protected Boolean doInBackground(Void... params)
  48. {
  49. Log.d("SearchTask", "doInBackground");
  50. if (!Network.NetworkIsAvailable())
  51. {
  52. errorcode = Network.RESULT_NET_NOTAVAILABLE;
  53. return false;
  54. }
  55. String jstr = "";
  56. jstr = Network.check_push();
  57. // if (module_name.equals("Announcements"))
  58. // jstr = Network.get_announcements(lastid, limit);
  59. // else
  60. // jstr = Network.get_marketnews(lastid, limit);
  61. if (jstr == null || jstr.length() <= 0)
  62. {
  63. // Log.d(TAG, "json is wrong");
  64. errorcode = Network.RESULT_NET_ERROR;
  65. return false;
  66. }
  67. content = jstr;
  68. return true;
  69. }
  70. @Override
  71. protected void onPostExecute(Boolean success)
  72. {
  73. Log.i("onPostExecute", "entry");
  74. m_task = null;
  75. // showProgress(false);
  76. // switch (errorcode)
  77. // {
  78. // // case Network.RESULT_NET_NOTAVAILABLE:
  79. // // {
  80. // // Toast toast = Toast.makeText(
  81. // // ApexTrackingApplication.get_instance(),
  82. // // getText(R.string.msg_connection_none),
  83. // // Toast.LENGTH_LONG);
  84. // // toast.setGravity(Gravity.CENTER, 0, 0);
  85. // // toast.show();
  86. // // break;
  87. // // }
  88. // // case Network.RESULT_NET_ERROR:
  89. // // {
  90. // // Toast toast = Toast.makeText(
  91. // // ApexTrackingApplication.get_instance(),
  92. // // getText(R.string.msg_net_error), Toast.LENGTH_LONG);
  93. // // toast.setGravity(Gravity.CENTER, 0, 0);
  94. // // toast.show();
  95. // // break;
  96. // // }
  97. // // case Network.RESULT_ERROR:
  98. // // // case Network.RESULT_RESPONSE_NULL:
  99. // // {
  100. // // Toast toast = Toast.makeText(
  101. // // ApexTrackingApplication.get_instance(),
  102. // // getText(R.string.msg_net_resulterror),
  103. // // Toast.LENGTH_LONG);
  104. // // toast.setGravity(Gravity.CENTER, 0, 0);
  105. // // toast.show();
  106. // // break;
  107. // // }
  108. //
  109. // default:
  110. // break;
  111. // }
  112. if (success)
  113. {
  114. JSONObject jsobj;
  115. //
  116. // array = new JSONArray(json);
  117. try
  118. {
  119. jsobj = new JSONObject(content);
  120. String message = jsobj.getString("message");
  121. String date = jsobj.getString("date");
  122. String s_id = jsobj.getString("s_id");
  123. String e_id = jsobj.getString("e_id");
  124. int count = jsobj.getInt("count");
  125. SQLiteDatabase db = dbUtil.OpenDB(
  126. ApexTrackingApplication.get_instance(), null, true);
  127. db.execSQL("insert into push_message(s_id,e_id,msgcount,message,h_time,user,create_time,read) values('"
  128. + s_id
  129. + "','"
  130. + e_id
  131. + "',"
  132. + count
  133. + ",'"
  134. + message
  135. + "','"
  136. + date
  137. + "','"
  138. + ApexTrackingApplication.get_user()
  139. + "',"
  140. + System.currentTimeMillis() + ",0)");
  141. dbUtil.CloseDB(db);
  142. boolean bnotify = mcontext.getSharedPreferences("setting",
  143. 0).getBoolean("notifications_new_message", true);
  144. if (bnotify)
  145. {
  146. boolean bsound = mcontext.getSharedPreferences("setting",
  147. 0).getBoolean("notifications_new_message_sound", true);
  148. boolean bvibrate = mcontext.getSharedPreferences("setting",
  149. 0).getBoolean("notifications_new_message_vibrate", true);
  150. NotificationManager nManager = (NotificationManager) mcontext
  151. .getSystemService(Context.NOTIFICATION_SERVICE);
  152. Notification notification = new Notification(
  153. R.drawable.ic_launcher,
  154. mcontext.getString(R.string.str_notification_title),
  155. System.currentTimeMillis());
  156. Intent intent = new Intent(mcontext,
  157. FunctionSelectActivity.class);
  158. intent.putExtra("launcher", "notification");
  159. PendingIntent pintent = PendingIntent.getActivity(
  160. mcontext, 0, intent,
  161. PendingIntent.FLAG_UPDATE_CURRENT);
  162. if(bsound&&bvibrate)
  163. notification.defaults = Notification.DEFAULT_ALL;
  164. else if(bsound)
  165. notification.defaults = Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS;
  166. else if(bvibrate)
  167. notification.defaults = Notification.DEFAULT_VIBRATE|Notification.DEFAULT_LIGHTS;
  168. notification.flags = Notification.FLAG_AUTO_CANCEL;
  169. notification.number = ApexTrackingApplication.ncount++;
  170. if (notification.number > 1)
  171. notification
  172. .setLatestEventInfo(
  173. mcontext,
  174. notification.number
  175. + ApexTrackingApplication
  176. .get_instance()
  177. .getString(
  178. R.string.str_mnotification_title),
  179. ApexTrackingApplication
  180. .get_instance()
  181. .getString(
  182. R.string.str_notification_text)
  183. + date, pintent);
  184. else
  185. notification
  186. .setLatestEventInfo(
  187. mcontext,
  188. ApexTrackingApplication
  189. .get_instance()
  190. .getString(
  191. R.string.str_notification_title),
  192. ApexTrackingApplication
  193. .get_instance()
  194. .getString(
  195. R.string.str_notification_text)
  196. + date, pintent);
  197. nManager.notify(R.layout.activity_apex, notification);
  198. }
  199. // Intent intenticon = new Intent();
  200. // intenticon.setClass(ApexTrackingApplication
  201. // .get_instance(), FunctionSelectActivity.class);
  202. // Intent addShortcut = new
  203. // Intent("com.android.launcher.action.INSTALL_SHORTCUT");
  204. // Parcelable icon =
  205. // Intent.ShortcutIconResource.fromContext(mcontext,
  206. // R.drawable.ic_launcher_new);
  207. // addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
  208. // ApexTrackingApplication
  209. // .get_instance().getString(R.string.app_name));
  210. // addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
  211. // intent);
  212. // addShortcut.putExtra("duplicate", 0);
  213. // addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
  214. // icon);
  215. // ApexTrackingApplication
  216. // .get_instance().sendBroadcast(addShortcut);
  217. // notification
  218. }
  219. catch (JSONException e)
  220. {
  221. // TODO Auto-generated catch block
  222. e.printStackTrace();
  223. }
  224. // if (bfinish)
  225. // {
  226. // // getListView().removeFooterView(view_page_footer);
  227. // // Toast.makeText(AnnouncementActivity.this, "Load all!",
  228. // // Toast.LENGTH_LONG).show();
  229. //
  230. // TextView tv = (TextView) view_page_footer
  231. // .findViewById(R.id.text_page);
  232. // tv.setText("No more items");
  233. // tv.setEnabled(false);
  234. // }
  235. // else
  236. // {
  237. // TextView tv = (TextView) view_page_footer
  238. // .findViewById(R.id.text_page);
  239. // tv.setText("More...");
  240. // tv.setEnabled(true);
  241. // }
  242. // adapter.notifyDataSetChanged();
  243. // // getListAdapter().notifyDataSetChanged;
  244. // // BaseAdapter mJson = null;
  245. // // mJson.notifyDataSetChanged();
  246. }
  247. super.onPostExecute(success);
  248. }
  249. @Override
  250. protected void onCancelled()
  251. {
  252. m_task = null;
  253. // showProgress(false);
  254. }
  255. }
  256. @Override
  257. public void onReceive(Context context, Intent intent)
  258. {
  259. String tag = "onReceive@Alarmreceiver";
  260. if (intent.getAction().equals("com.usai.apex.push"))
  261. {
  262. // Bundle b = intent.getExtras();
  263. // Set<String> keySet = b.keySet();
  264. // for(String key : keySet) {
  265. // Log.e(tag, key);
  266. // }
  267. // String s =b.getString("caller");
  268. Log.d(tag,
  269. "receive alarm broadcast caller =="
  270. + intent.getStringExtra("caller"));
  271. checkpush(context);
  272. // Intent i = new Intent();
  273. // i.setClass(context, DaemonService.class);
  274. // // 启动service
  275. // // 多次调用startService并不会启动多个service 而是会多次调用onStart
  276. // context.startService(i);
  277. }
  278. else if (intent.getAction().equals("com.usai.apex.push.cancel"))
  279. {
  280. if (m_task != null)
  281. m_task.cancel(true);
  282. NotificationManager nManager = (NotificationManager) context
  283. .getSystemService(Context.NOTIFICATION_SERVICE);
  284. nManager.cancel(R.layout.activity_apex);
  285. ApexTrackingApplication.cancelalarm();
  286. // ApexTrackingApplication.put_password("");
  287. // ApexTrackingApplication.put_sessionid("");
  288. // ApexTrackingApplication.put_user("");
  289. ApexTrackingApplication.logout();
  290. }
  291. }
  292. }