Guest User

Untitled

a guest
Oct 7th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. public class backWorkerNotifTom extends AsyncTask<String,String,String> {
  2. Context context;
  3. String result;
  4. String[] BookTittle;
  5. public backWorkerNotifTom(Context context){this.context = context;}
  6. @Override
  7. protected String doInBackground(String... params) {
  8. String type = params[0];
  9. String NotifTomURL = "http://192.168.254.120/LibrayAPI/SelectNotifTom.php";
  10. if (type.equals("SelectNotifTom")){
  11. String dateTom = params[1];
  12. String borrowerID = params[2];
  13.  
  14. try {
  15. String data = URLEncoder.encode("date_tom","UTF-8") + "=" +
  16. URLEncoder.encode(dateTom,"UTF-8");
  17. data += "&" + URLEncoder.encode("borrower_id","UTF-8") + "=" +
  18. URLEncoder.encode(borrowerID,"UTF-8");
  19.  
  20. URL url = new URL(NotifTomURL);
  21. URLConnection urlConnection = url.openConnection();
  22. urlConnection.setDoOutput(true);
  23.  
  24. OutputStreamWriter outputStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream());
  25. outputStreamWriter.write(data);
  26. outputStreamWriter.flush();
  27.  
  28. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  29.  
  30. StringBuilder sb = new StringBuilder();
  31. String line;
  32.  
  33. while ((line = bufferedReader.readLine())!= null){
  34. sb.append(line+"n");
  35. }
  36. result = sb.toString();
  37.  
  38. JSONArray jsonArray = new JSONArray(result);
  39. JSONObject jsonObject;
  40. BookTittle = new String[jsonArray.length()];
  41. for (int i = 0; i < jsonArray.length(); i++) {
  42. jsonObject = jsonArray.getJSONObject(i);
  43. BookTittle[i] = jsonObject.getString("BookTittle");
  44. // PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, NotifList.class), 0);
  45. NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(context)
  46. .setSmallIcon(R.drawable.ic_book_black_24dp)
  47. .setContentTitle("Library Alert")
  48. .setContentText("Tommorow is the Due Day of the Book " + BookTittle[i] + " You Borrowed")
  49.  
  50. //.setContentIntent(pendingIntent)
  51. .setTicker("Notifications");
  52.  
  53. NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
  54.  
  55. if (notificationManager != null) {
  56. notificationManager.notify(i, mbuilder.build());
  57. }
  58. }
  59. }
  60. catch (Exception ex){
  61. ex.printStackTrace();
  62.  
  63. }
  64.  
  65.  
  66. }
  67. return result;
  68. }
  69.  
  70. @Override
  71. protected void onPreExecute() {
  72. super.onPreExecute();
  73. }
  74.  
  75. @Override
  76. protected void onPostExecute(String s) {
  77. super.onPostExecute(s);
  78.  
  79. }
  80. }
  81.  
  82. public class backWorkerNotif extends AsyncTask<String, String, String> {
  83. String result = null;
  84. String[] BookTittle;
  85. Context context;
  86. public backWorkerNotif(Context context){this.context = context;}
  87.  
  88. @Override
  89. protected String doInBackground(String... params) {
  90. String selectNotif_url = "http://192.168.254.120/LibrayAPI/SelectNotif.php";
  91. String type = params[0];
  92.  
  93. if (type.equals("Notif")) {
  94. String DateNow = params[1];
  95. String BorrowerID = params[2];
  96. try {
  97.  
  98. String data = URLEncoder.encode("date_now", "UTF-8") + "=" +
  99. URLEncoder.encode(DateNow, "UTF-8");
  100. data += "&" + URLEncoder.encode("borrower_id", "UTF-8") + "=" +
  101. URLEncoder.encode(BorrowerID, "UTF-8");
  102.  
  103. URL url = new URL(selectNotif_url);
  104. URLConnection urlConnection = url.openConnection();
  105. urlConnection.setDoOutput(true);
  106.  
  107. OutputStreamWriter outputStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream());
  108.  
  109. outputStreamWriter.write(data);
  110. outputStreamWriter.flush();
  111.  
  112. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  113. StringBuilder sb = new StringBuilder();
  114. String line;
  115. while ((line = bufferedReader.readLine()) != null) {
  116.  
  117. sb.append(line + "n");
  118. }
  119. result = sb.toString();
  120. if (result.isEmpty()) {
  121. } else {
  122. //Json
  123.  
  124. JSONArray jsonArray = new JSONArray(result);
  125. JSONObject jsonObject;
  126. BookTittle = new String[jsonArray.length()];
  127. for (int i = 0; i < jsonArray.length(); i++) {
  128. jsonObject = jsonArray.getJSONObject(i);
  129. BookTittle[i] = jsonObject.getString("BookTittle");
  130. // PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, NotifList.class), 0);
  131. NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(context)
  132. .setSmallIcon(R.drawable.ic_book_black_24dp)
  133. .setContentTitle("Library notification")
  134. .setContentText("Today is the Due Day of the Book " + BookTittle[i] + " You Borrowed")
  135.  
  136. //.setContentIntent(pendingIntent)
  137. .setTicker("Notification Alert");
  138.  
  139. NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
  140.  
  141. if (notificationManager != null) {
  142. notificationManager.notify(i, mbuilder.build());
  143. }
  144. }
  145. }
  146. }
  147. catch (Exception ex) {
  148. ex.printStackTrace();
  149.  
  150. }
  151.  
  152.  
  153. }
  154.  
  155. return null;
  156. }
  157.  
  158. @Override
  159. protected void onPreExecute() {
  160. super.onPreExecute();
  161. }
  162.  
  163. @Override
  164. protected void onPostExecute(String jsonArray) {
  165. super.onPostExecute(jsonArray);
  166.  
  167. }
  168. }
  169.  
  170. public class Login extends AppCompatActivity {
  171.  
  172. EditText username,password;
  173.  
  174. @Override
  175. protected void onCreate(Bundle savedInstanceState) {
  176. super.onCreate(savedInstanceState);
  177. setContentView(R.layout.activity_login);
  178.  
  179. username = findViewById(R.id.edtUsername);
  180. password = findViewById(R.id.edtPassword);
  181. }
  182.  
  183. public void onLogin(View view) {
  184.  
  185. String Username = username.getText().toString();
  186. String Password = password.getText().toString();
  187. //notif call
  188. //call notif for overdue tommorow
  189. backWorkerNotifTom backWorkerNotifTom = new backWorkerNotifTom(this);
  190. //get datetime tom
  191. Calendar calendar = Calendar.getInstance();
  192. calendar.add(Calendar.DAY_OF_YEAR,1);
  193. Date dateTom = calendar.getTime();
  194. SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
  195. String dateTomString = sf1.format(dateTom);
  196. Toast.makeText(this, dateTomString, Toast.LENGTH_SHORT).show();
  197. backWorkerNotifTom.execute("SelectNotifTom",dateTomString,Username);
  198.  
  199. backWorkerNotif backWorkerNotif = new backWorkerNotif(this);
  200. Date date = Calendar.getInstance().getTime();
  201. SimpleDateFormat SF = new SimpleDateFormat("yyyy-MM-dd");
  202. String DateNow = SF.format(date);
  203. backWorkerNotif.execute("Notif", DateNow, Username);
  204.  
  205. String Type = "login";
  206. GlobalVariable.BorrowerID = Username;
  207. GlobalVariable.Password = Password;
  208. backgroundWorker _backgroundWorker = new backgroundWorker(this);
  209. _backgroundWorker.execute(Type, Username, Password);
  210.  
  211.  
  212. }
  213. }
Add Comment
Please, Sign In to add comment