I want to post JSON string with HttpURLConnection api to the localhost server(WAMP) periodically every 60 seconds to be inserted in the database. Therefore, I am executing MyAs
You can create Service for your purpose and for every 60 seconds, you need not use Alarm Manager. Because Service is providing intent filter functionality to call service after every Minute.
for that you have to write following code when you will register your service:
ServiceDemo serviceDemo = new ServiceDemo();
IntentFilter s_intentFilter = new IntentFilter();
s_intentFilter.addAction(Intent.ACTION_TIME_TICK);
s_intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
registerReceiver(serviceDemo , s_intentFilter);
Service will execute after every 60 seconds using this code.
Personally, i would use the alarm manager. That's what its for.
It is none of my business, but you should keep in mind that a request over the internet once every minute will drain your battery.
first we create a service like this
public class ChatSevice extends IntentService{
public ChatSevice() {
super("Some");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
//this is asynk task class
new ChatConnect(ChatSevice.this).execute();
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
}
}
and now call this service like this way
Intent intent3 = new Intent(this, ChatSevice.class);
PendingIntent pintent3 = PendingIntent.getService(this, 0, intent3, 0);
AlarmManager alarm3 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// for 30 mint 60*60*1000
alarm3.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
1000, pintent3);
startService(new Intent(getBaseContext(), ChatSevice.class));