Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7706613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:10:01+00:00 2026-06-01T00:10:01+00:00

i have already finished code to passing data to url , and it did

  • 0

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" />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:10:02+00:00Added an answer on June 1, 2026 at 12:10 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just finished reading this post: https://developer.yahoo.com/performance/rules.html#flush and have already implemented a flush after
I have already created a webpart to show the data from list, but I
I have already posted a question about this, but the situation has changed sufficiently
I have already posted something similar here but I would like to ask the
I have already googled for this I have a Table with following structure in
The title explains all... I have this snippet of code in my application: String
I have this code below #include <stdio.h> int main(){ int i=0; for(i=0; i<1000; i++)
I have used GTMOAuth to successfully login to dropbox but i dont seem to
There is a very common task I face again. I have already solved this
I've posted about this last year because some university project and now I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.