通过将toast实例保留在android的旧版本中,可以解决重复显示问题。 这既适用于普通app,也适用于系统app
if (空值==toast ) {
toast=toast.maketext(mcontext,id,Toast.LENGTH_SHORT );
} else {
toast.settext(id;
toast.set duration (toast.length _ short;
}
toast.show (;
但是到了安卓9.0,这就不太合适了。 由于Toast代码发生了变化,因此系统优化此问题的常见app解决方案是直接应用toast.maketext(…).show )进行显示,但系统app ) Android.uid.syketext (…. show )
虽然通过查看源代码发现系统来区分APP,但系统APP应用和非系统APP应用不是一种方法:
在otificationmanagerservice.enqueue toast方法中
.
finalbooleanissystemtoast=iscallersystemorphone (|(Android '.equals ) pkg );
finalbooleanispackagesuspended=
ispackagesuspendedforuser(pkg,Binder.getCallingUid ) );
.
同步(mtoastqueue ) {
intcallingpid=binder.getcallingpid (;
longcallingid=binder.clearcallingidentity (;
try {
ToastRecord record;
索引;
slog.I(tag,' issystemtoast : ' issystemtoast ',ispackagesuspended 3360 ' ispackagesuspended );
//allpackagesasidefromtheandroidpackagecanenqueueonetoastatatime
if (! isSystemToast ) {
index=indexoftoastpackagelocked (pkg;
} else {
index=indexoftoastlocked(pkg,callback );
}
//If the package already has a toast,we update its toast
//in the queue,we don ' tmoveittotheendofthequeue。
sog.I(tag,' index:' index );
if(index=0) {
record=mtoastqueue.get(index;
record.update(duration;
try {
record.callback.hide (;
}catch(remoteexceptione ) {
}
record.update(callback;
} else {
Binder token=new Binder (;
mwindowmanagerinternal.addwindowtoken (token,TYPE_TOAST,DEFAULT_DISPLAY );
record=newtoastrecord (calling PID,pkg,callback,duration,token );
mtoastqueue.add(record;
index=mtoastqueue.size((-1;
}
keepprocessaliveifneededlocked (calling PID;
//If it's at index 0,it ' sthecurrenttoast.itdoesn ' tmatterifit ' s
//neworjustbeenupdated.callbackandtellittoshowitself。
//If the callback fails,this will remove it from the list,so don't
//assume that it's valid after th
is.if (index == 0) {
showNextToastLocked();
}
} finally {
Binder.restoreCallingIdentity(callingId);
}
}
其中index小于0会新建一个record,否则就会使用队列里已经有的,就是这样达到优化的目的。我加上log运行发现我的应用每次都是-1,普通app就第一次是-1.后面都是0。所以每次都是重新创建的record.
为了尽量少修改源码,在应用中做特殊处理,每次显示时把原来的taost取消再显示,这样虽然还是新建record,但是也能缩短显示时间
public static void show(Context context, int id) {
if (null == context)
return;
mContext = context.getApplicationContext();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
if (null == toast) {
toast = Toast.makeText(mContext, id, Toast.LENGTH_SHORT);
} else {
toast.setText(id);
toast.setDuration(Toast.LENGTH_SHORT);
}
toast.show();
} else {
if (null != toast) {
toast.cancel();
toast = null;
}
toast = Toast.makeText(mContext, id, Toast.LENGTH_SHORT);
toast.show();
}
}
完整代码:
import android.content.Context;
import android.os.Build;
import android.widget.Toast;
import 你的Application;
public class ToastUtils {
private static Toast toast;
private static Context mContext;
public static void show(String text, int duration) {
show(MyApplication.getApplication().getApplicationContext(), text, duration);
}
public static void show(String text) {
show(MyApplication.getApplication().getApplicationContext(), text);
}
public static void show(int id, int duration) {
show(MyApplication.getApplication().getApplicationContext(), id, duration);
}
public static void show(int id) {
show(MyApplication.getApplication().getApplicationContext(), id);
}
public static void show(Context context, String text, int duration) {
if (null == context)
return;
mContext = context.getApplicationContext();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
if (null == toast) {
toast = Toast.makeText(mContext, text, duration);
} else {
toast.setText(text);
toast.setDuration(duration);
}
toast.show();
} else {
if (null != toast) {
toast.cancel();
toast = null;
}
toast = Toast.makeText(mContext, text, duration);
toast.show();
}
}
public static void show(Context context, String text) {
if (null == context)
return;
mContext = context.getApplicationContext();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
if (null == toast) {
toast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
} else {
toast.setText(text);
toast.setDuration(Toast.LENGTH_SHORT);
}
toast.show();
} else {
if (null != toast) {
toast.cancel();
toast = null;
}
toast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
toast.show();
}
}
public static void show(Context context, int id, int duration) {
if (null == context)
return;
mContext = context.getApplicationContext();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
if (null == toast) {
toast = Toast.makeText(mContext, id, duration);
} else {
toast.setText(id);
toast.setDuration(duration);
}
toast.show();
} else {
if (null != toast) {
toast.cancel();
toast = null;
}
toast = Toast.makeText(mContext, id, duration);
toast.show();
}
}
public static void show(Context context, int id) {
if (null == context)
return;
mContext = context.getApplicationContext();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
if (null == toast) {
toast = Toast.makeText(mContext, id, Toast.LENGTH_SHORT);
} else {
toast.setText(id);
toast.setDuration(Toast.LENGTH_SHORT);
}
toast.show();
} else {
if (null != toast) {
toast.cancel();
toast = null;
}
toast = Toast.makeText(mContext, id, Toast.LENGTH_SHORT);
toast.show();
}
}
}