-3

Please guide me in this. Appreciate all your help.

My background service is toasting ABC

//-------------------String displayingText = "ABC";-----------------

And I have two strings, ABC and DEF declared in mainactivity.java

  1. How do I pass the value displayingText from main activity to this service.
  2. How do I change the displayingText to DEF after the toast ABC finished.

MyService.Java

public class MyService extends Service {
public static final long INTERVAL=3000;//variable to execute services every 5 second
private Handler mHandler=new Handler(); // run on another Thread to avoid crash
private Timer mTimer=null; // timer handling

//the get intent dont work. where or how should i put it?

Intent myIntent = getIntent(); 
    if (myIntent !=null && myIntent.getExtras()!=null)
String value = myIntent.getExtras().getString(PassToService);

@Nullable
@Override
public IBinder onBind(Intent intent) {
    throw new UnsupportedOperationException("unsupported Operation");
}
@Override
public void onCreate() {


    // cancel if service is  already existed
    if(mTimer!=null)
        mTimer.cancel();
    else
        mTimer=new Timer(); // recreate new timer
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(),0,INTERVAL);// schedule task

}


    @Override
public void onTaskRemoved(Intent rootIntent) {
    stopSelf();///its will stop service
    super.onTaskRemoved(rootIntent);
}
@Override
public void onDestroy() {
    Toast.makeText(this, "In Destroy", Toast.LENGTH_SHORT).show();//display toast when method called
    mTimer.cancel();//cancel the timer
    super.onDestroy();

}

//inner class of TimeDisplayTimerTask
private class TimeDisplayTimerTask extends TimerTask {
    @Override
    public void run() {

        // run on another thread
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                // display toast at every 10 second
                //String displayingText = "ABC";
                String displayingText = myIntent.getStringExtra("PassToService");
                final Toast Notify = Toast.makeText(getApplicationContext(), displayingText, Toast.LENGTH_SHORT);
                Notify.setGravity(Gravity.CENTER, 0, 0);
                Notify.show();

                Handler cancelToast = new Handler();
                cancelToast.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Notify.cancel();
                    }
                }, 1000);

            }
        });
    }
}

}

Anthony
  • 51
  • 7
  • 1
    Possible duplicate of [How to Display toast in AsyncTask in android](https://stackoverflow.com/questions/16830255/how-to-display-toast-in-asynctask-in-android) – Ken Y-N Dec 05 '17 at 04:36
  • The variable is to be read from file and should not be using asynctask – Anthony Dec 05 '17 at 06:03

1 Answers1

0

You can do it by passing value from activity to service-

startService(new Intent(YourActivity.Service.class).putExtra("key","value"));

Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18
  • Can we actually getIntent() method inside a service? I've tried https://stackoverflow.com/questions/3293243/pass-data-from-activity-to-service-using-an-intent But inside service intent.getIntExtra Didnt work – Anthony Dec 05 '17 at 06:14
  • @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null && intent.hasExtra("key")) { String i = intent.getStringExtra("key", ""); } Try This – Shivam Oberoi Dec 05 '17 at 06:19
  • Could you advice again where can i put the getstringextra from the file i uploaded? – Anthony Dec 05 '17 at 07:13