简体   繁体   中英

from Service, call Activity method (if it's in the foreground)

From an Android Service , I would like to call an Activity method, but only if the Activity is in the foreground .

I would like nothing to happen in case the Activity is not in the foreground.

Which is the simplest way to achieve that?

From a Service always better to broadcast events if the activity is listening to that broadcast will respond. If the activity is not listening then nothing will happen it will ignore.

This is the better solution than the one which you have asked.

I found a very simple solution, adapted from this previous answer :

On the Service :

Intent intent = new Intent(MainActivity.RECEIVER_INTENT);
intent.putExtra(MainActivity.RECEIVER_MESSAGE, myMessage);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

On the Activity :

public static final String RECEIVER_INTENT = "RECEIVER_INTENT";
public static final String RECEIVER_MESSAGE = "RECEIVER_MESSAGE";

Create a listener on onCreate() :

public void onCreate(Bundle savedInstanceState) {
    ...
    mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String message = intent.getStringExtra(RECEIVER_MESSAGE);
            // call any method you want here
        }
    };
}

register it in onStart() :

@Override
protected void onStart() {
    super.onStart();
    LocalBroadcastManager.getInstance(this).registerReceiver((mBroadcastReceiver), 
        new IntentFilter(RECEIVER_INTENT)
    );
}

unregister it in onStop() :

@Override
protected void onStop() {
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
    super.onStop();
}

You can do this using a Interface just to check if the activity is in background or in foreground.

I am sharing some code to have some idea.

public interface CheckAppInForeground {

boolean isAppInForGround();
}

In your Activity

public class MainActivity extends AppCompatActivity implements CheckAppInForeground {

 Boolean isAppInForeGround;

  @Override
protected void onResume() {
    super.onResume();
    isAppInForeGround = true;
}

@Override
protected void onStop() {
    super.onStop();
    isAppInForeGround = false;
}

@Override
public boolean isAppInForGround() {
    return isAppInForeGround;
}
}

And your service class

public class MyService extends Service {
Activity activity;

public MyService(Activity activity) {
    this.activity = activity;
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    MainActivity mainActivity = (MainActivity) activity;
    if (activity != null) {
        boolean isActivityInForGround = mainActivity.isAppInForGround();
        if (isActivityInForGround) {
            // Do what you want to do when activity is in foreground
        } else {
            // Activity is in background
        }
    } else {
        // Activity is destroyed
    }
    return super.onStartCommand(intent, flags, startId);
}
}

I think i am clear with the code. If you find something missing or unclear please let me know

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM