- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我想在我的 Android 应用程序中显示一个应用程序覆盖屏幕。就像带有停机时间消息的屏幕。此屏幕应覆盖我的应用程序中的其他 fragment/Activity 。我怎样才能做到这一点?
最佳答案
**1. Grant overlay permission of your appp that can show overlay layout.**
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)){
private void systemOverlay(){
//If the draw over permission is not available open the settings screen
//to grant the permission.
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
}}
**2. Start Overlay Service**
Intent intent = new Intent(this, NotificationHeadService.class);
Bundle bundle = new Bundle();
intent.putExtra("pNewsId", pNewsId);
startService(intent);
**3. Layout**
remove.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/remove_relativelayout"
android:background="@drawable/chat_background">
<RelativeLayout
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_centerInParent="true"
android:background="@drawable/chat_head_cross_bac">
<ImageView
android:layout_width="34dp"
android:layout_height="34dp"
android:id="@+id/remove_img"
android:layout_centerInParent="true"
android:src="@drawable/ic_cross_chat_head"
/>
</RelativeLayout>
</RelativeLayout>
notificationhead.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="67dp"
android:layout_height="67dp"
android:id="@+id/chathead_relativelayout"
android:background="@drawable/chat_corner"
>
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:id="@+id/chathead_img"
android:adjustViewBounds="true"
android:src="@drawable/ic_chat_head"
android:visibility="visible"
android:layout_centerInParent="true"
android:padding="2dp"
android:background="@drawable/chat_round_corner"
/>
</RelativeLayout>
3. Implementaion Like facebook messenger circle notification
import java.util.Date;
public class NotificationHeadService extends Service {
private WindowManager windowManager;
private RelativeLayout chatheadView, removeView;
private LinearLayout txtView, txt_linearlayout;
private ImageView chatheadImg, removeImg;
private int x_init_cord, y_init_cord, x_init_margin, y_init_margin;
private Point szWindow = new Point();
private boolean isLeft = true;
private String sMsg = "";
Bundle bundle = null;
String pNewsId = "";
String pushType = "";
String pNewsImage = "";
String pNewsPaperId = "";
String custom_content="";
int LAYOUT_FLAG;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void handleStart(){
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
removeView = (RelativeLayout)inflater.inflate(R.layout.remove, null);
final WindowManager.LayoutParams paramRemove = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT);
paramRemove.gravity = Gravity.TOP | Gravity.LEFT;
removeView.setVisibility(View.GONE);
removeImg = (ImageView)removeView.findViewById(R.id.remove_img);
windowManager.addView(removeView, paramRemove); //Ad Remove vIew
chatheadView = (RelativeLayout) inflater.inflate(R.layout.notificationhead, null);
chatheadImg = (ImageView)chatheadView.findViewById(R.id.chathead_img);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
windowManager.getDefaultDisplay().getSize(szWindow);
} else {
int w = windowManager.getDefaultDisplay().getWidth();
int h = windowManager.getDefaultDisplay().getHeight();
szWindow.set(w, h);
}
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 150;
windowManager.addView(chatheadView, params);
chatheadView.setOnTouchListener(new View.OnTouchListener() {
long time_start = 0, time_end = 0;
boolean isLongclick = false, inBounded = false;
int remove_img_width = 0, remove_img_height = 0;
Handler handler_longClick = new Handler();
Runnable runnable_longClick = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
isLongclick = true;
removeView.setVisibility(View.VISIBLE);
chathead_longclick();
}
};
@Override
public boolean onTouch(View v, MotionEvent event) {
WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) chatheadView.getLayoutParams();
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
int x_cord_Destination, y_cord_Destination;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
time_start = System.currentTimeMillis();
handler_longClick.postDelayed(runnable_longClick, 200);
remove_img_width = removeImg.getLayoutParams().width;
remove_img_height = removeImg.getLayoutParams().height;
x_init_cord = x_cord;
y_init_cord = y_cord;
x_init_margin = layoutParams.x;
y_init_margin = layoutParams.y;
break;
case MotionEvent.ACTION_MOVE:
int x_diff_move = x_cord - x_init_cord;
int y_diff_move = y_cord - y_init_cord;
x_cord_Destination = x_init_margin + x_diff_move;
y_cord_Destination = y_init_margin + y_diff_move;
if(isLongclick){
int x_bound_left = szWindow.x / 2 - (int)(remove_img_width * 1.5);
int x_bound_right = szWindow.x / 2 + (int)(remove_img_width * 1.5);
int y_bound_top = szWindow.y - (int)(remove_img_height * 1.5);
if((x_cord >= x_bound_left && x_cord <= x_bound_right) && y_cord >= y_bound_top){
inBounded = true;
int x_cord_remove = (int) ((szWindow.x - (remove_img_height * 1.5)) / 2);
int y_cord_remove = (int) (szWindow.y - ((remove_img_width * 1.5) + getStatusBarHeight() ));
if(removeImg.getLayoutParams().height == remove_img_height){
removeImg.getLayoutParams().height = (int) (remove_img_height * 1.5);
removeImg.getLayoutParams().width = (int) (remove_img_width * 1.5);
WindowManager.LayoutParams param_remove = (WindowManager.LayoutParams) removeView.getLayoutParams();
param_remove.x = x_cord_remove;
param_remove.y = y_cord_remove;
windowManager.updateViewLayout(removeView, param_remove);
}
layoutParams.x = x_cord_remove + (Math.abs(removeView.getWidth() - chatheadView.getWidth())) / 2;
layoutParams.y = y_cord_remove + (Math.abs(removeView.getHeight() - chatheadView.getHeight())) / 2 ;
windowManager.updateViewLayout(chatheadView, layoutParams);
break;
}else{
inBounded = false;
removeImg.getLayoutParams().height = remove_img_height;
removeImg.getLayoutParams().width = remove_img_width;
WindowManager.LayoutParams param_remove = (WindowManager.LayoutParams) removeView.getLayoutParams();
int x_cord_remove = (szWindow.x - removeView.getWidth()) / 2;
int y_cord_remove = szWindow.y - (removeView.getHeight() + getStatusBarHeight() );
param_remove.x = x_cord_remove;
param_remove.y = y_cord_remove;
windowManager.updateViewLayout(removeView, param_remove);
}
}
layoutParams.x = x_cord_Destination;
layoutParams.y = y_cord_Destination;
windowManager.updateViewLayout(chatheadView, layoutParams);
break;
case MotionEvent.ACTION_UP:
isLongclick = false;
removeView.setVisibility(View.GONE);
removeImg.getLayoutParams().height = remove_img_height;
removeImg.getLayoutParams().width = remove_img_width;
handler_longClick.removeCallbacks(runnable_longClick);
if(inBounded){
stopService(new Intent(NotificationHeadService.this, NotificationHeadService.class));
inBounded = false;
break;
}
int x_diff = x_cord - x_init_cord;
int y_diff = y_cord - y_init_cord;
if(Math.abs(x_diff) < 5 && Math.abs(y_diff) < 5){
time_end = System.currentTimeMillis();
if((time_end - time_start) < 300){
windowManager.removeView(chatheadView);
chathead_click();
}
}
else {
y_cord_Destination = y_init_margin + y_diff;
int BarHeight = getStatusBarHeight();
if (y_cord_Destination < 0) {
y_cord_Destination = 0;
} else if (y_cord_Destination + (chatheadView.getHeight() + BarHeight) > szWindow.y) {
y_cord_Destination = szWindow.y - (chatheadView.getHeight() + BarHeight);
}
layoutParams.y = y_cord_Destination;
}
inBounded = false;
resetPosition(x_cord);
break;
default:
break;
}
return true;
}
});
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
if(windowManager == null)
return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
windowManager.getDefaultDisplay().getSize(szWindow);
} else {
int w = windowManager.getDefaultDisplay().getWidth();
int h = windowManager.getDefaultDisplay().getHeight();
szWindow.set(w, h);
}
WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) chatheadView.getLayoutParams();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
if(txtView != null){
txtView.setVisibility(View.GONE);
}
if(layoutParams.y + (chatheadView.getHeight() + getStatusBarHeight()) > szWindow.y){
layoutParams.y = szWindow.y- (chatheadView.getHeight() + getStatusBarHeight());
windowManager.updateViewLayout(chatheadView, layoutParams);
}
if(layoutParams.x != 0 && layoutParams.x < szWindow.x){
resetPosition(szWindow.x);
}
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
if(txtView != null){
txtView.setVisibility(View.GONE);
}
if(layoutParams.x > szWindow.x){
resetPosition(szWindow.x);
}
}
}
private void resetPosition(int x_cord_now) {
if (x_cord_now <= szWindow.x / 2) {
isLeft = true;
moveToLeft(x_cord_now);
} else {
isLeft = false;
moveToRight(x_cord_now);
}
}
private void moveToLeft(final int x_cord_now){
final int x = szWindow.x - x_cord_now;
new CountDownTimer(500, 5) {
WindowManager.LayoutParams mParams = (WindowManager.LayoutParams) chatheadView.getLayoutParams();
public void onTick(long t) {
if (chatheadView.getWindowToken() != null) {
long step = (500 - t) / 5;
mParams.x = 0 - (int) (double) bounceValue(step, x);
windowManager.updateViewLayout(chatheadView, mParams);
}
}
public void onFinish() {
if (chatheadView.getWindowToken() != null) {
mParams.x = 0;
windowManager.updateViewLayout(chatheadView, mParams);
}
}
}.start();
}
private void moveToRight(final int x_cord_now){
new CountDownTimer(500, 5) {
WindowManager.LayoutParams mParams = (WindowManager.LayoutParams) chatheadView.getLayoutParams();
public void onTick(long t) {
if (chatheadView.getWindowToken() != null) {
long step = (500 - t) / 5;
mParams.x = szWindow.x + (int) (double) bounceValue(step, x_cord_now) - chatheadView.getWidth();
windowManager.updateViewLayout(chatheadView, mParams);
}
}
public void onFinish() {
if (chatheadView.getWindowToken() != null) {
mParams.x = szWindow.x - chatheadView.getWidth();
windowManager.updateViewLayout(chatheadView, mParams);
}
}
}.start();
}
private double bounceValue(long step, long scale){
double value = scale * Math.exp(-0.055 * step) * Math.cos(0.08 * step);
return value;
}
private int getStatusBarHeight() {
int statusBarHeight = (int) Math.ceil(25 * getApplicationContext().getResources().getDisplayMetrics().density);
return statusBarHeight;
}
private void chathead_click(){
PendingIntent contentIntent=null;
Date now = new Date();
long uniqueId = now.getTime();
Intent intent = new Intent(this, PushDetailsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
contentIntent = PendingIntent.getActivity(this,
(int) uniqueId, intent.putExtra("pNewsId", pNewsId)
.putExtra("pNewsPaperId", pNewsPaperId),
PendingIntent.FLAG_UPDATE_CURRENT);
try {
contentIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
private void chathead_longclick(){
WindowManager.LayoutParams param_remove = (WindowManager.LayoutParams) removeView.getLayoutParams();
int x_cord_remove = (szWindow.x - removeView.getWidth()) / 2;
int y_cord_remove = szWindow.y - (removeView.getHeight() + getStatusBarHeight() );
param_remove.x = x_cord_remove;
param_remove.y = y_cord_remove;
windowManager.updateViewLayout(removeView, param_remove);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
if(intent != null){
final Bundle bundle = intent.getExtras();
pNewsId = bundle.getString("pNewsId", "");
pushType = bundle.getString("pushType", "");;
pNewsImage = bundle.getString("pNewsImage", "");
pNewsPaperId = bundle.getString("pNewsPaperId", "");
custom_content="";
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
RingtoneManager.getRingtone(this, alarmSound).play();
}
if(startId == Service.START_STICKY) {
handleStart();
return super.onStartCommand(intent, flags, startId);
}else{
return Service.START_NOT_STICKY;
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(chatheadView != null && chatheadView.getWindowToken() != null){
windowManager.removeView(chatheadView);
}
if(txtView != null && chatheadView.getWindowToken() != null){
windowManager.removeView(txtView);
}
if(removeView != null && chatheadView.getWindowToken() != null){
windowManager.removeView(removeView);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
链接:https://www.androidhive.info/2016/11/android-floating-widget-like-facebook-chat-head/
关于android - 如何在我的 Android 应用程序中显示应用程序覆盖屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57036161/
我的Angular-Component位于一个flexbox(id =“log”)中。可以显示或隐藏flexbox。 我的组件内部有一个可滚动区域,用于显示日志消息。 (id =“message-li
我真的很困惑 有一个 phpinfo() 输出: MySQL 支持 启用 客户端 API 版本 5.5.40 MYSQL_MODULE_TYPE 外部 phpMyAdmin 显示: 服务器类型:Mar
我正在研究这个 fiddle : http://jsfiddle.net/cED6c/7/我想让按钮文本在单击时发生变化,我尝试使用以下代码: 但是,它不起作用。我应该如何实现这个?任何帮助都会很棒
我应该在“dogs_cats”中保存表“dogs”和“cats”各自的ID,当看到数据时显示狗和猫的名字。 我有这三个表: CREATE TABLE IF NOT EXISTS cats ( id
我有一个字符串返回到我的 View 之一,如下所示: $text = 'Lorem ipsum dolor ' 我正在尝试用 Blade 显示它: {{$text}} 但是,输出是原始字符串而不是渲染
我无法让我的链接(由图像表示,位于页面左侧)真正有效地显示一个 div(包含一个句子,位于中间)/单击链接时隐藏。 这是我的代码: Practice
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
最初我使用 Listview 来显示 oracle 结果,但是最近我不得不切换到 datagridview 来处理比 Listview 允许的更多的结果。然而,自从切换到数据网格后,我得到的结果越来越
我一直在尝试插入一个 Unicode 字符 ∇ 或 ▽,所以它显示在 Apache FOP 生成的 PDF 中。 这是我到目前为止所做的: 根据这个基本帮助 Apache XSL-FO Input,您
我正在使用 node v0.12.7 编写一个 nodeJS 应用程序。 我正在使用 pm2 v0.14.7 运行我的 nodejs 应用程序。 我的应用程序似乎有内存泄漏,因为它从我启动时的大约 1
好的,所以我有一些 jQuery 代码,如果从下拉菜单中选择了带有前缀 Blue 的项目,它会显示一个输入框。 代码: $(function() { $('#text1').hide();
当我试图检查 Chrome 中的 html 元素时,它显示的是 LESS 文件,而 Firefox 显示的是 CSS 文件。 (我正在使用 Bootstrap 框架) 如何在 Chrome 中查看 c
我是 Microsoft Bot Framework 的新手,我正在通过 youtube 视频 https://youtu.be/ynG6Muox81o 学习它并在 Ubuntu 上使用 python
我正在尝试转换从 mssql 生成的文件到 utf-8。当我打开他的输出 mssql在 Windows Server 2003 中使用 notepad++ 将文件识别为 UCS-2LE我使用 file
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我正在尝试执行单击以打开/关闭一个 div 的功能。 这是基本的,但是,点击只显示 div,当我点击“关闭”时,没有任何反应。 $(".inscricao-email").click(function
假设我有 2 张卡片,屏幕上一次显示一张。我有一个按钮可以用其他卡片替换当前卡片。现在假设卡 1 上有一些数据,卡 2 上有一些数据,我不想破坏它们每个上的数据,或者我不想再次重建它们中的任何一个。
我正在使用 Eloquent Javascript 学习 Javascript。 我在 Firefox 控制台上编写了以下代码,但它返回:“ReferenceError:show() 未定义”为什么?
我正在使用 Symfony2 开发一个 web 项目,我使用 Sonata Admin 作为管理面板,一切正常,但我想要做的是,在 Sonata Admin 的仪表板菜单上,我需要显示隐藏一些菜单取决
我试图显示一个div,具体取决于从下拉列表中选择的内容。例如,如果用户从列表中选择“现金”显示现金div或用户从列表中选择“检查”显示现金div 我整理了样本,但样本不完整,需要接线 http://j
我是一名优秀的程序员,十分优秀!