55-Android之修改Toast的显示时长

55-Android之修改Toast的显示时长

平台:SRPD Android10 SC9832E

在系统中,输入法切换的提示,我使用的是Toast的来实现的。

后续客户测试反映,输入法切换的提示显示太慢,要求改进。

我因此针对输入法的Toast的显示,增加了一个Toast.LENGTH_INPUT属性。

frameworks/base/core/java/android/widget/Toast.java

@IntDef(prefix = { "LENGTH_" }, value = {
     		LENGTH_SHORT,
            LENGTH_LONG,
            LENGTH_INPUT // TODO 添加
})
@Retention(RetentionPolicy.SOURCE)
public @interface Duration {}

public static final int LENGTH_INPUT = 2;  // TODO 添加

frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java 

@GuardedBy("mToastQueue")
private void scheduleDurationReachedLocked(ToastRecord r){
    mHandler.removeCallbacksAndMessages(r);
    Message m = Message.obtain(mHandler, MESSAGE_DURATION_REACHED, r);
    // TODO 此处修改 @{
    // int delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY;
    int delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : (r.duration == Toast.LENGTH_INPUT ? 800 : SHORT_DELAY);
    // @}
    // Accessibility users may need longer timeout duration. This api compares original delay
    // with user's preference and return longer one. It returns original delay if there's no
    // preference.
    delay = mAccessibilityManager.getRecommendedTimeoutMillis(delay,
            AccessibilityManager.FLAG_CONTENT_TEXT);
    mHandler.sendMessageDelayed(m, delay);
}

如果需要修改Toast的Toast.LENGTH_SHORT和Toast. LENGTH_LONG的默认时长,请修改NotificationManagerService中LONG_DELAY和SHORT_DELAY常量的值:

frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java 

static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT;
static final int SHORT_DELAY = 2000; // 2 seconds,TODO 修改该值


frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

public static final int TOAST_WINDOW_TIMEOUT = 3500; // 3.5 seconds, TODO 修改该值
代码实现 playmouse.java package com.example.playmouse; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import java.util.Random; public class playmouse extends AppCompatActivity { /************1.定义变量、对象、洞穴坐标******************/ private int i=0;//记录打到的地鼠个数 private ImageView mouse;//定义 mouse 对象 private TextView info1; //定义 info1 对象(用于查看洞穴坐标) private Handler handler;//声明一个 Handler 对象 public int[][] position=new int[][]{ {277, 200}, {535, 200}, {832, 200}, {1067,200}, {1328, 200}, {285, 360}, {645, 360}, {1014,360}, {1348, 360},{319, 600},{764, 600},{1229,600} };//创建一个表示地鼠位置的数组 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置不显示顶部栏 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置横屏模式 /************2.绑定控件*****************/ mouse = (ImageView) findViewById(R.id.imageView1); info1 = findViewById(R.id.info); /************获取洞穴位置*****************/ //通过 logcat 查看 【注】:getRawY():触摸点距离屏幕上方的长度(此长度包括程序项目名栏的) info1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: float x = event.getRawX(); float y = event.getRawY(); Log.i("x:" + x, "y:" + y); break; default: break; } return false; } }); /************3.实现地鼠随机出现*****************/ //创建 Handler 消息处理机制 handler = new Handler() { @Override public void handleMessage(@NonNull Message msg) { //需要处理的消息 int index; if (msg.what == 0x101) { index = msg.arg1;//// 获取位置索引值 mouse.setX(position[index][0]);//设置 X 轴坐标 mouse.setY(position[index][1]);//设置 Y 轴坐标(原点为屏幕左上角(不包括程序名称栏)) mouse.setVisibility(View.VISIBLE);//设置地鼠显示 } super.handleMessage(msg); } }; // 创建线程 Thread t = new Thread(new Runnable() { @Override public void run() { int index = 0;// 定义一个记录地鼠位置的索引值 while (!Thread.currentThread().isInterrupted()) { index = new Random().nextInt(position.length);// 产生一个随机整数(范围:0<=index<数组长度) Message m = handler.obtainMessage();//创建消息对象 m.what = 0x101;//设置消息标志 m.arg1 = index;// 保存地鼠标位置的索引值 handler.sendMessage(m);// 发送消息通知 Handler 处理 try { Thread.sleep(new Random().nextInt(500) + 500); // 休眠一段时间 } catch (InterruptedException e) { e.printStackTrace(); } } } }); t.start(); /************4.实现点击地鼠后的事件:让地鼠不显示&显示消息*****************/ // 添加触摸 mouse 后的事件 mouse.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { v.setVisibility(View.INVISIBLE);//设置地鼠不显示 i++; Toast.makeText(playmouse.this, "打到[ " + i + " ]只地鼠!", Toast.LENGTH_SHORT).show(); // 显示消息提示框 return false; } }); }} 一键获取完整项目代码 java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 activity_main.xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fl" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" > <ImageView android:id="@+id/imageView1" android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/mouse1" /> <TextView android:id="@+id/info" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </FrameLayout> 一键获取完整项目代码 java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 styles.xml(把顶部通知栏去掉) <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources> 一键获取完整项目代码 java,能否详细的写出步骤,以及手把手教学
最新发布
11-13
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值