-1

I am trying to call api for every 1second in onHandleIntent method of ServiceIntent class in android..

onHandleIntent is executed only Once, I want to run this method for every one Second..

FancyBookMakingService.class

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

            if (intent != null) {
                try {
                    Thread.sleep(1000);

                    String result = getApi(URL); //API calling
                    Intent intentFBM = new Intent(DataHolder.ACTION_SEND_DATA);
                    intentFBM.putExtra("key", result);
                    sendBroadcast(intentFBM);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

    }

BroadcastReceiver

I have two ServiceIntent Class, first one is working fine another one not & BroadcastReceiverSignalr is a Innerclass

public class BroadcastReceiverSignalr extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (action.equalsIgnoreCase(DataHolder.ACTION_SEND_ACTIVE)) {
                String result = intent.getStringExtra(DataHolder.keySIGNALR);
                Log.i("TAG",result);

            }else if (action.equalsIgnoreCase(DataHolder.ACTION_SEND_DATA)){
                String data = intent.getStringExtra("key");
                Log.i("TAG",data);
            }
        }
    }

AndroidMainfest.xml

<service android:name=".FancyBookMakingService" />
Fra Red
  • 420
  • 5
  • 13
  • 1
    Looking at the code this should be the resultant behavior of `IntentService`. When requests have been handled, the `IntentService` stops itself .Ref [onHandleIntent](https://developer.android.com/reference/android/app/IntentService#onhandleintent). – ADM May 08 '18 at 05:28

1 Answers1

0

Use Service class instead of IntentService and use timer like

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService(new Intent(this,Service.class)); //start service which is Service.java
}

}

and your service class look like rhis

public class Service extends Service {

public static final int notify = 1000;   Service run every 1 seconds)
private Handler mHandler = new Handler();  
private Timer mTimer = null;   

@Override
public IBinder onBind(Intent intent) {
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {

    mTimer = new Timer();   //recreate new
    mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify);   //Schedule task
}

@Override
public void onDestroy() {
    super.onDestroy();
    mTimer.cancel();    //For Cancel Timer
    Toast.makeText(this, "Service is Destroyed", Toast.LENGTH_SHORT).show();
}

//class TimeDisplay for handling task
class TimeDisplay extends TimerTask {
    @Override
    public void run() {
        // run on another thread
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                // display toast
                Log.d("service is ","running");
            }
        });
    }
}
Rahul Chaudhary
  • 1,059
  • 1
  • 6
  • 15
  • Can I call **mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify);** in onHandlerIntent() function @Rahul Chaudhary – Fra Red May 08 '18 at 12:08
  • for reference referhttps://stackoverflow.com/questions/30216423/is-it-a-good-practice-to-use-timertask-in-onhandleintent-in-intentservice – Rahul Chaudhary May 08 '18 at 12:13
  • I have tried your above code but it excute only once, I want to run it continuously @Rahul Chaudhary – Fra Red May 09 '18 at 08:02
  • use Log instead of toast in run() method . this will work – Rahul Chaudhary May 09 '18 at 08:35
  • @FraRed Service uses UI thread so you can not call Api in run() method directly. – Rahul Chaudhary May 09 '18 at 11:01
  • Now run() method is working fine continuously, but when I try to call API in the run() Method I am getting android.os.NetworkOnMainThreadException `public void run() { result = DataHolder.getApi(url); Log.i("TAG"," "+result); }` `Error: 05-09 16:15:40.921 /com.example D/ERROR: android.os.NetworkOnMainThreadException 05-09 16:15:40.921 /com.example I/TAG: ` @Rahul Chaudhary – Fra Red May 09 '18 at 11:03
  • you can follow this https://stackoverflow.com/questions/19617621/android-how-to-send-http-request-via-service-every-10-seconds – Rahul Chaudhary May 09 '18 at 11:06
  • I've refer above Link my issues is resolve thanks for your help @Rahul Chaudhar – Fra Red May 09 '18 at 13:54