Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Intent ll24 = new Intent(getBaseContext(), AlarmReceiver.class);
- PendingIntent recurringLl24 = PendingIntent.getBroadcast(getBaseContext(), 0, ll24, PendingIntent.FLAG_CANCEL_CURRENT);
- AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
- alarms.setRepeating(AlarmManager.RTC_WAKEUP,0, 30*10*100, recurringLl24);
- }
- //.....
- }
- public class AlarmReceiver extends WakefulBroadcastReceiver
- {
- static Context context;
- Date curTime,sdf,edf;
- private static final String LOG_TAG = "ForegroundService";
- @Override
- public void onReceive(Context context, Intent intent)
- {
- try
- {
- Calendar c = Calendar.getInstance();
- curTime = new SimpleDateFormat( "HH:mm" ).parse(c.get( Calendar.HOUR_OF_DAY)+":"+c.get( Calendar.MINUTE));
- sdf = new SimpleDateFormat( "HH:mm" ).parse("08:00");
- edf = new SimpleDateFormat( "HH:mm" ).parse("20:00");
- if(curTime.after(sdf) && curTime.before(edf))
- {
- Intent ll24Service = new Intent(context, ForegroundService.class);
- ll24Service.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
- ForegroundService.IS_SERVICE_RUNNING = true;
- startWakefulService(context,ll24Service);
- }
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public class ForegroundService extends Service
- {
- ...
- @Override
- public int onStartCommand(Intent intent, int flags, int startId)
- {
- AlarmReceiver.completeWakefulIntent(intent);
- if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION))
- {
- Log.i(LOG_TAG, "Received Start Foreground Intent ");
- showNotification();
- Calendar c = Calendar.getInstance();
- 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);
- gps = new GPS(ForegroundService.this);
- if(gps.canGetLocation())
- {
- double latitude = gps.getLatitude();
- double longitude = gps.getLongitude();
- latt=Double.toString(latitude);
- longi =String.valueOf(longitude);
- insertData();
- }
- else
- {
- gps.showSettingsAlert();
- }
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- Log.i(LOG_TAG, "In onDestroy");
- }
- private void insertData()
- {
- try
- {
- String QUERY = "insert into GpsLoc values('"+usr+"','"+latt+"','"+longi+"','"+curDateTime+"')";
- myDatabase.execSQL(QUERY);
- Log.i(LOG_TAG, "insert");
- ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
- Boolean isInternetPresent = cd.isConnectingToInternet();
- if(isInternetPresent)
- {
- Log.i(LOG_TAG, "chk int");
- addToJason();
- }
- else
- {
- Log.i(LOG_TAG, "No int");
- stopForeground(true);
- stopSelf();
- }
- }
- catch(Exception ex)
- {
- Toast.makeText(getApplicationContext(), "Data not inserted : "+ex.toString(), Toast.LENGTH_LONG).show();
- }
- }
Add Comment
Please, Sign In to add comment