[英]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 . 从Android 服务 ,我想调用Activity方法,但前提是该Activity位于前台 。
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. 如果某个活动正在收听广播,则从某个Service广播事件总是更好 。 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()
: 在
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()
: 在
onStart()
注册它:
@Override
protected void onStart() {
super.onStart();
LocalBroadcastManager.getInstance(this).registerReceiver((mBroadcastReceiver),
new IntentFilter(RECEIVER_INTENT)
);
}
unregister it in onStop()
: 在
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. 您可以使用“
Interface
来执行此操作,只是检查活动是在后台还是在前台。
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
如果您发现缺少或不清楚的地方,请告诉我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.