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 7687249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:39:44+00:00 2026-05-31T19:39:44+00:00

I have the code below and I want to loop this every 5 seconds.

  • 0

I have the code below and I want to loop this every 5 seconds. I have tried to use broadcastreceiver and alarm manager but without success . I want my service to loop for 5 times and after that stop permanently .This is the second question I post . Please somebody help me to fix the code, I am stacked on my mac for 2 days. Any help will be appreciated.

Code to loop every 5 seconds

File key = new File(Environment.getExternalStorageDirectory(),"newfile"+Math.random()+".txt");
try {
    key.createNewFile();
} catch (IOException e) {
    e.printStackTrace();
}

InfoReceiver.class Service

public class InfoReceiver extends Service {

    @Override
    public void onCreate()
    {
        AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        Calendar time = Calendar.getInstance();
        time.setTimeInMillis(System.currentTimeMillis());
        time.add(Calendar.SECOND, 5);
        alarmMgr.set(AlarmManager.RTC_WAKEUP, 5000, pendingIntent);
    }
}

InfoManager.class service

public  class InfoManager extends Service {
    @Override
    public void onCreate() {
        File key = new File(Environment.getExternalStorageDirectory(),"newfile"+Math.random()+".txt");
        try {
            key.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

MyBroadcastreceiver.class

public class MyBroadcastReceiver extends BroadcastReceiver {
    private Context mContext;
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, InfoManager.class);
        context.startService(startServiceIntent);
    }
}

Manifest Code

<service
    android:name=".InfoReceiver"
    android:enabled="true" >
</service>
<service
    android:name=".InfoManager"
    android:enabled="true" >
    <intent-filter>
        <action android:name=".InfoManager" />
    </intent-filter>
</service>
<receiver
    android:name=".MyBroadcastReceiver"
    android:process=":remote" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
  • 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-05-31T19:39:45+00:00Added an answer on May 31, 2026 at 7:39 pm

    Use Handler.postDelayed(Runnable r, long delayMillis). You don’t need timers or the alarm manager to do what you want. Stay simple.

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

Sidebar

Related Questions

I want to open an xlsx file, I have tried the below code,but neither
I have this perl code below, basically want to loop through these 2 files.
I have the code below with 3 columns. I want to have the border
I have to code below - works great in IE and Opera, but does
This code below causes infinate loop problem (as documented). So how do I set
I have this code below. It works fine in safari however in IE the
I have python code below that will loop through a table and print out
I have code below: <select id=testSelect> <option value=1>One</option> <option value=2>Two</option> </select> <asp:Button ID=btnTest runat=server
I have the code below : public class Anything { public int Data {
I have the code below that hides and shows the navigational bar. It is

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.