简体   繁体   English

Android 工作室 Java。 在后台显示一个弹窗 window

[英]Android Studio Java. Display a pop-up window in the background

How do I display a popup when an app is in the background?当应用程序在后台时如何显示弹出窗口? For example, I took the GetContact application, when a call is received from the user, a window with information about the call pops up on the screen.例如,我使用 GetContact 应用程序,当收到用户的呼叫时,屏幕上会弹出一个带有呼叫信息的 window。 How can this be done?如何才能做到这一点? It is desirable with code examples, I will be very grateful.最好有代码示例,我将不胜感激。 Thank.感谢。 enter image description here在此处输入图像描述

  • Add required permissions in your app manifest file:在您的应用清单文件中添加所需的权限:

     <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  • Incoming call reciever来电接听者

    public class IncomingCallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { try { System.out.println("Receiver start"); String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { Toast.makeText(context, "Incoming Call State", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Ringing State Number is -" + incomingNumber, Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } }
  • Ask Runtime permission in your MainActivity.在 MainActivity 中询问运行时权限。

  • Add overlay view to display notification in screen.添加覆盖视图以在屏幕中显示通知。

     WindowManager wm =(WindowManager)getSystemService(Context.WINDOW_SERVICE); View view = LayoutInflater.from(context).inflate(R.layout.test, null) wm.addView(view, mParams)

What you're looking for is Drawing over other apps.您正在寻找的是在其他应用程序上绘图。 It's a tricky feature of android that I personally love.这是我个人喜欢的 android 的一个棘手功能。 You have to deal with WindowManager and you need SYSTEM_ALERT_WINDOW permission您必须处理 WindowManager 并且需要 SYSTEM_ALERT_WINDOW 权限

Implementations are huge and I guess it's better to give you some links.实现是巨大的,我想最好给你一些链接。

Here's a good explanation about floating widgets https://medium.com/@kevalpatel2106/create-chat-heads-like-facebook-messenger-32f7f1a62064这是关于浮动小部件https://medium.com/@kevalpatel2106/create-chat-heads-like-facebook-messenger-32f7f1a62064的一个很好的解释

This is kinda close to what you want: https://github.com/codebyshubham/Calling_Person_History这有点接近你想要的: https://github.com/codebyshubham/Calling_Person_History

Also remember that custom roms like Xiaomi have some restrictions on these widgets.还要记住,像小米这样的自定义 rom 对这些小部件有一些限制。 Also a great open source floating app is called QuickLyric take a look at it it might help you a lot还有一个很棒的开源浮动应用程序叫做 QuickLyric 看看它可能对你有很大帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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