Guest User

Untitled

a guest
Mar 19th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6.  
  7. Intent ll24 = new Intent(getBaseContext(), AlarmReceiver.class);
  8. PendingIntent recurringLl24 = PendingIntent.getBroadcast(getBaseContext(), 0, ll24, PendingIntent.FLAG_CANCEL_CURRENT);
  9. AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  10. alarms.setRepeating(AlarmManager.RTC_WAKEUP,0, 30*10*100, recurringLl24);
  11. }
  12. //.....
  13. }
  14.  
  15. public class AlarmReceiver extends WakefulBroadcastReceiver
  16. {
  17. static Context context;
  18. Date curTime,sdf,edf;
  19. private static final String LOG_TAG = "ForegroundService";
  20.  
  21. @Override
  22. public void onReceive(Context context, Intent intent)
  23. {
  24. try
  25. {
  26. Calendar c = Calendar.getInstance();
  27. curTime = new SimpleDateFormat( "HH:mm" ).parse(c.get( Calendar.HOUR_OF_DAY)+":"+c.get( Calendar.MINUTE));
  28. sdf = new SimpleDateFormat( "HH:mm" ).parse("08:00");
  29. edf = new SimpleDateFormat( "HH:mm" ).parse("20:00");
  30. if(curTime.after(sdf) && curTime.before(edf))
  31. {
  32. Intent ll24Service = new Intent(context, ForegroundService.class);
  33. ll24Service.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
  34. ForegroundService.IS_SERVICE_RUNNING = true;
  35. startWakefulService(context,ll24Service);
  36. }
  37. } catch (ParseException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. }
  41. }
  42. }
  43.  
  44. public class ForegroundService extends Service
  45. {
  46. ...
  47. @Override
  48. public int onStartCommand(Intent intent, int flags, int startId)
  49. {
  50. AlarmReceiver.completeWakefulIntent(intent);
  51. if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION))
  52. {
  53. Log.i(LOG_TAG, "Received Start Foreground Intent ");
  54. showNotification();
  55.  
  56. Calendar c = Calendar.getInstance();
  57. curDateTime = c.get( Calendar.YEAR)+"-"+ String.valueOf(c.get( Calendar.MONTH )+1)+"-"+ c.get( Calendar.DAY_OF_MONTH)+" "+c.get( Calendar.HOUR_OF_DAY)+":"+c.get( Calendar.MINUTE)+":"+c.get( Calendar.SECOND);
  58.  
  59. gps = new GPS(ForegroundService.this);
  60. if(gps.canGetLocation())
  61. {
  62. double latitude = gps.getLatitude();
  63. double longitude = gps.getLongitude();
  64. latt=Double.toString(latitude);
  65. longi =String.valueOf(longitude);
  66.  
  67. insertData();
  68. }
  69. else
  70. {
  71. gps.showSettingsAlert();
  72. }
  73. }
  74.  
  75. @Override
  76. public void onDestroy() {
  77. super.onDestroy();
  78. Log.i(LOG_TAG, "In onDestroy");
  79. }
  80.  
  81. private void insertData()
  82. {
  83. try
  84. {
  85. String QUERY = "insert into GpsLoc values('"+usr+"','"+latt+"','"+longi+"','"+curDateTime+"')";
  86. myDatabase.execSQL(QUERY);
  87.  
  88. Log.i(LOG_TAG, "insert");
  89. ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
  90. Boolean isInternetPresent = cd.isConnectingToInternet();
  91.  
  92. if(isInternetPresent)
  93. {
  94. Log.i(LOG_TAG, "chk int");
  95. addToJason();
  96. }
  97. else
  98. {
  99. Log.i(LOG_TAG, "No int");
  100. stopForeground(true);
  101. stopSelf();
  102. }
  103.  
  104.  
  105.  
  106. }
  107. catch(Exception ex)
  108. {
  109. Toast.makeText(getApplicationContext(), "Data not inserted : "+ex.toString(), Toast.LENGTH_LONG).show();
  110. }
  111. }
Add Comment
Please, Sign In to add comment