[英]Starting Activity from background and hiding MainActivity - Popup Window
I was wondering if it is possible to start Acitivty from service running in background and then move MainActivity to background. 我想知道是否有可能从后台运行的服务启动Acitivty,然后将MainActivity移至后台。 I don't want to finish() MainActivity.
我不想完成()MainActivity。 Just hide it.
藏起来
Intent it=new Intent(context, NewPopupRecognizer.class);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
I tried this code but it is always starting Activity with delay. 我尝试了这段代码,但始终会延迟启动Activity。
I want to create popup Activity which i can turn on/off with floating button. 我想创建一个弹出活动,可以用浮动按钮打开/关闭它。 I used to use WindowManger but it was very problematic so I decided to try doing it with Activity.
我曾经使用WindowManger,但是这非常麻烦,因此我决定尝试使用Activity。
The popup should be like: Facebook Messenger or Google Assistant. 弹出窗口应类似于:Facebook Messenger或Google Assistant。
I have read this in another post that this may help you: 我在另一篇文章中阅读了这篇文章,这可能对您有所帮助:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(this, NewPopupRecognizer.class);
intent.setComponent(cn);
startActivity(intent);
found here How to start an Activity from a Service? 在此处找到如何从服务启动活动? service/3456099
服务/ 3456099
What you can do is to send a Broadcast to your activity from the service: 您可以做的是通过该服务向您的活动发送广播 :
Intent intent = new Intent("com.yourcompany.testIntent");
intent.putExtra("value","test");
sendBroadcast(intent);
Then your MainActivity
picks it up and does: 然后,您的
MainActivity
拾取并执行以下操作:
IntentFilter filter = new IntentFilter("com.yourcompany.testIntent");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Intent it=new Intent(context, NewPopupRecognizer.class);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
it.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
startActivity(it);
}
};
registerReceiver(receiver, filter);
You can add a mechanism that will open the activity directly from the service if the main activity is not there. 您可以添加一种机制,如果主要活动不存在,则可以直接从服务中打开活动。 For more options check out the answers to this question .
有关更多选项,请查看此问题的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.