【问题标题】:I want to show gif image show on screen even when app is closed即使应用程序关闭,我也想在屏幕上显示 gif 图像
【发布时间】:2018-09-18 07:28:08
【问题描述】:

通过鼠标点击,您实现了两件事:

  • 正在播放声音
  • GIF 图像正在显示

我正在尝试在屏幕上显示 GIF,即使我关闭了正在使用服务的应用程序,但由于在出现 GIF 图像时声音一直在播放,我找不到解决方案。

TheService.java(代码)

package gallery.suitapps.catwalking;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import pl.droidsonroids.gif.GifImageView;

public class TheService extends Service  {

    final class MyThreadClass implements Runnable{
        int service_id;
        MyThreadClass(int service_id){
            this.service_id=service_id;
        }

        @Override
        public void run() {

        }
    }

    SoundPool soundPool = null;
    private int[] f10685s;


    MainActivity main = new MainActivity();

    public MediaPlayer player;
    Context context;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Toast.makeText(this,"Service Starts" , Toast.LENGTH_SHORT).show();
        player = MediaPlayer.create(this,R.raw.sound2);
        player.setLooping(true);
        player.start();

        MainActivity.imageView.setImageResource(R.drawable.cat_gif);

        return START_STICKY;

    }

    @Override
    public void onDestroy() {
        Toast.makeText(this,"Service Stops" , Toast.LENGTH_SHORT).show();
        player.stop();
        MainActivity.imageView.setImageResource(R.drawable.ic_launcher_background);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

MainActivity.java(代码)

package gallery.suitapps.catwalking;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import pl.droidsonroids.gif.GifImageView;

public class MainActivity extends AppCompatActivity {

    public static GifImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.catgif);
    }

    public void startService (View view){

        Intent intent = new Intent(this,TheService.class);
        startService(intent);

       // imageView.setImageResource(R.drawable.cat_gif);

    }

    public void stopService (View view){

        Intent intent = new Intent(this,TheService.class);
        stopService(intent);

    }


}

activity_main.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">

    <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>

【问题讨论】:

  • 检查我的答案,让我知道它是否有效
  • 是的,它可以工作,但不完全是我想要的,它将整个应用程序屏幕浮动在手机屏幕上,而我只希望 GIF 浮动在屏幕上。
  • 好的,你要显示屏幕的哪个部分?
  • 在我的回答中检查我的更新:)

标签: android service android-service java-threads android-thread


【解决方案1】:

根据您的要求,您需要实现系统窗口 服务类中的警报,以在应用程序运行时在后台显示图像 关闭

这是满足您要求的解决方案。

manifest添加这个

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

然后在您的启动器activity 或任何基本活动中添加这些权限

 private static final int CODE_DRAW_OVER_OTHER_APP_PERMISSION = 2084;

onCreate()添加这个

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 
            }
        }
    }

并在service 类中执行此操作,检查下面的链接并分别完成您的工作

   https://medium.com/@kevalpatel2106/create-chat-heads-like-facebook-messenger-32f7f1a62064

在哪里image_layout。 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>

更新:- 您可以在 image_layout 文件中进行更改,以在屏幕上显示任何大小和重力的 GIF 您可以使用

进行更改
android:layout_width="any_size"
    android:layout_height="any_size"

【讨论】:

    猜你喜欢
    • 2019-08-01
    • 1970-01-01
    • 2013-08-25
    • 2014-10-30
    • 2021-12-10
    • 2019-02-20
    • 2021-09-08
    • 2019-04-24
    • 1970-01-01
    相关资源
    最近更新 更多