Android app中只有一个service 要显示toast

目前在做一个类似360悬浮球的app,整个项目只有一个Service,在其中进行了view的显示、点击、接收数据等操作。

但在某些情况下想要弹出一个toast,但查来查去,网上都是使用以下代码实现:

Handler handler=new Handler(Looper.getMainLooper());
            handler.post(new Runnable(){
                public void run(){
                    Toast.makeText(getApplicationContext() ,"显示Toast在屏幕上!",Toast.LENGTH_LONG).show();
          }
            });

其原理是通过Handler将一个自定义的线程运行于主线程之上,以此来显示,但我的项目中根本没有activity,这条路根本不行。

我想到两个方法

  1. 启动一个透明的Activity,onCreate方法中显示tost,然后定时finish。
  2. 使用WindowManager来显示一个view,然后定时移除。

我是用的第二种方法:

private void showToast(Context context){
        final WindowManager wManager=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        final TextView textView=new TextView(context);
        textView.setBackgroundResource(R.drawable.shape_bg_ball);
        textView.setText(context.getString(R.string.toClipboard));
        textView.setPadding(dp2Px(10,context),dp2Px(5,context),dp2Px(10,context),dp2Px(5,context));
        int screenHeigth = wManager.getDefaultDisplay().getHeight();

        int mStatusBarHeight = dp2Px(25, context);

        WindowManager.LayoutParams mParamsToast = new WindowManager.LayoutParams();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT > 23) {
                    //在android7.1以上系统需要使用TYPE_PHONE类型 配合运行时权限
                mParamsToast.type = WindowManager.LayoutParams.TYPE_PHONE;
            } else {
                mParamsToast.type = WindowManager.LayoutParams.TYPE_TOAST;
            }
        } else {
            mParamsToast.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        
        mParamsToast.format = PixelFormat.RGBA_8888;
        mParamsToast.gravity = Gravity.LEFT | Gravity.TOP;
        mParamsToast.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        int defaultY =screenHeigth- ((screenHeigth - mStatusBarHeight) / 2) / 3;
        mParamsToast.x = wManager.getDefaultDisplay().getWidth()/2- Tools.getViewWidth(textView)/2;

        mParamsToast.y = defaultY;
        mParamsToast.alpha = 1;
        mParamsToast.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mParamsToast.height = WindowManager.LayoutParams.WRAP_CONTENT;

        wManager.addView(textView, mParamsToast);
        new Handler().postDelayed(()->
                wManager.removeViewImmediate(textView),500);
    }

先new一个TextView,给它设置背景与颜色、字号,使之符合你想要的toast,然后设置好LayoutParams,把textView add到windowmanager上,然后定时移除。

Android的`Toast`通常用于快速向用户显示简短的信息,但它默认不会全屏显示。如果需要模拟全屏效果,可以采用一些技巧。一种常见的做法是自定义一个全屏Dialog或者Activity,然后将`Toast`的内容添加到这个全屏容器。 首先,创建一个全屏的布局文件(如fullscreen_layout.xml),包含一个TextView或者其他适合显示文本的组件: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/fullscreen_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:textColor="#fff" android:background="#000" /> </LinearLayout> ``` 然后,在Activity或Fragment使用`WindowManager`来显示这个全屏的布局: ```java import android.app.Activity; import android.content.Context; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.Toast; import android.view.WindowManager; public class CustomToast { private static final int LENGTH_LONG = Toast.LENGTH_LONG; private static final int LENGTH_SHORT = Toast.LENGTH_SHORT; public static void fullScreenToast(Context context, String message, int duration) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.fullscreen_layout, null); TextView textView = layout.findViewById(R.id.fullscreen_text_view); textView.setText(message); WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_TOAST, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT ); params.gravity = Gravity.CENTER垂直方向|Gravity.CENTER水平方向; ((Activity) context).getWindow().addContentView(layout, params); if (duration == LENGTH_SHORT) { // 短时间展示 Handler handler = new Handler(); handler.postDelayed(() -> ((Activity) context).getWindow().removeView(layout), duration); } else { // 长时间展示 Toast.makeText(context, message, LENGTH_LONG).cancel(); } } } ``` 使用时只需调用`CustomToast.fullScreenToast(context, "你的消息", LENGTH_SHORT / LENGTH_LONG)`即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值