Android 我想在屏幕上显示gif图像显示,即使应用程序关闭
只需单击鼠标,即可实现两件事:Android 我想在屏幕上显示gif图像显示,即使应用程序关闭,android,service,android-service,java-threads,android-thread,Android,Service,Android Service,Java Threads,Android Thread,只需单击鼠标,即可实现两件事: 声音正在播放 GIF图像正在显示 我试图在屏幕上显示GIF,即使在我关闭应用程序时,我正在使用这些服务,但由于GIF图像出现时声音一直在播放,我找不到解决方案 service.java(代码) MainActivity.java(代码) activity_main.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout x
- 声音正在播放
- GIF图像正在显示
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/startService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Start Service"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.134"
android:onClick="startService"/>
<Button
android:id="@+id/stopService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Stop Service"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.362"
android:onClick="stopService"/>
<pl.droidsonroids.gif.GifImageView
android:id="@+id/catgif"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher_background"
android:background="@android:color/holo_blue_dark"
/>
</android.support.constraint.ConstraintLayout>
根据您的要求,您需要实现系统窗口
当应用程序启动时,服务类中的警报将在后台显示图像
封闭的
这是满足您需求的解决方案
在manifest
中添加此
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
if (SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
//finish();
}
在onCreate()
中添加此
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
if (SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
//finish();
}
处理请求权限
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CODE_DRAW_OVER_OTHER_APP_PERMISSION) {
if (SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
} else {
// do stuff here
}
}
}
然后在服务
类中执行此操作检查下面的链接并分别执行您的操作
https://medium.com/@kevalpatel2106/create-chat-heads-like-facebook-messenger-32f7f1a62064
其中图像\u布局
。xml是
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<pl.droidsonroids.gif.GifImageView
android:id="@+id/catgif"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher_background"
android:background="@android:color/holo_blue_dark"
/>
</android.support.constraint.ConstraintLayout>
检查我的答案,让我知道它是否有效。是的,它有效,但不是我想要的,它在手机屏幕上浮动整个应用程序屏幕,而我只希望GIF在屏幕上浮动。好的,你想在屏幕的哪个部分显示它?在我的答案中检查我的更新:)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<pl.droidsonroids.gif.GifImageView
android:id="@+id/catgif"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher_background"
android:background="@android:color/holo_blue_dark"
/>
</android.support.constraint.ConstraintLayout>
android:layout_width="any_size"
android:layout_height="any_size"