Issue
i have already finished code to passing data to url , and it did successfully . but now i need to this in the background ,So i make a service to do this even the application is ended . my problem when i started the service it pass data once ,but i need to pass data every interval of time so How to do that ??
this is my code
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
Random r;
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
r=new Random();
//
int o=r.nextInt(1000);
HttpPost postMethod = new HttpPost("http://androidsaveitem.appspot.com/save");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("description+", "description FOR id "+String.valueOf(o)));
formparams.add(new BasicNameValuePair("id+", String.valueOf(o)));
UrlEncodedFormEntity entity;
try {
entity = new UrlEncodedFormEntity(formparams);
postMethod.setEntity(entity);
DefaultHttpClient hc = new DefaultHttpClient();
try {
HttpResponse response = hc.execute(postMethod);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
in the activity i write this
startService(new Intent(this, MyService.class));
i have already write the permission in the manifest file
<service android:enabled="true" android:name=".MyService" />
<uses-permission android:name="android.permission.INTERNET" />
Solution
Rather than leaving your Service
running in the background all the time (unless it's constantly doing work!), you should probably consider registering an alarm with the OS that will wake up your service at specified intervals and then let it go back to sleep when it's finished.
This will simplify your Service
implementation and free you from most of the timing related issues. You will also get the benefit that if your Service
crashes or is killed for some reason, the alarm timer will bring it back on the next cycle.
The way it works is by creating a BroadcastReceiver
that is listening for a particular Intent
that you've registered with Android's AlarmManager
.
Which option you choose will largely depend on how big your intervals are, since broadcasting an alarm is much more expensive than having a timer wait. If you want to do your work every few seconds, the timer is probably better. If your wait times are measured in minutes or hours, do the alarm thing.
First, you'll need to create the alarm.
PendingIntent pi = PendingIntent.getService(context, 0, yourIntent, yourFlags);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setInexactRepeating(AlarmManger.ELAPSED_REALTIME, // Alarm type
5000, // Time before first alarm
AlarmManager.INTERVAL_HOUR, // Alarm interval
pi); // Sent when alarm happens
To actually make use of the alarm, you have to listen for it to go off. For that, you need to create a BroadcastReceiver
.
public class SnoozeButton extends BroadcastReceiver {
public void onReceive (Context context, Intent intent) {
if (thisIntentIsTheOneIWant(intent){
context.startService(intent);
}
}
private boolean thisIntentIsTheOneIWant(Intent intent) {
// Test here to make sure this intent is for your app
// and service. You may get Intents other than what
// you are interested in.
}
}
Note that you must declare your BroadcastReciever
in your AndroidManifest.xml
.
Answered By - Argyle
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.