在android 低版本上保持提個toast實例即可解決重復(fù)顯示問題,這個對普通app和系統(tǒng)app都適用
if (null == toast) {
toast = Toast.makeText(mContext, id, Toast.LENGTH_SHORT);
} else {
toast.setText(id);
toast.setDuration(Toast.LENGTH_SHORT);
}
toast.show();
但是到了android 9.0,這樣就不太適用,因為Toast代碼已經(jīng)改變,系統(tǒng)對這個問題進行了優(yōu)化普通應(yīng)用解決方法就是直接適用Toast.makeText(...).show()去顯示,但是我遇到系統(tǒng)app(android.uid.system)使用單例時前兩個還能顯示,后面的就直接消失不見了,使用原生方法還是會顯示很久,不是說系統(tǒng)已經(jīng)優(yōu)化了嗎?
看了源碼發(fā)現(xiàn)系統(tǒng)對應(yīng)用進行了區(qū)分,系統(tǒng)應(yīng)用和非系統(tǒng)應(yīng)用走的不是一個方法:
NotificationManagerService.enqueueToast方法中
...
final boolean isSystemToast = isCallerSystemOrPhone() || ("android".equals(pkg));
final boolean isPackageSuspended =
isPackageSuspendedForUser(pkg, Binder.getCallingUid());
...
synchronized (mToastQueue) {
int callingPid = Binder.getCallingPid();
long callingId = Binder.clearCallingIdentity();
try {
ToastRecord record;
int index;
Slog.i(TAG, "isSystemToast:" + isSystemToast + ",isPackageSuspended:" + isPackageSuspended);
// All packages aside from the android package can enqueue one toast at a time
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't move it to the end of the queue.
Slog.i(TAG, "index:" + index);
if (index >= 0) {
record = mToastQueue.get(index);
record.update(duration);
try {
record.callback.hide();
} catch (RemoteException e) {
}
record.update(callback);
} else {
Binder token = new Binder();
mWindowManagerInternal.addWindowToken(token, TYPE_TOAST, DEFAULT_DISPLAY);
record = new ToastRecord(callingPid, pkg, callback, duration, token);
mToastQueue.add(record);
index = mToastQueue.size() - 1;
}
keepProcessAliveIfNeededLocked(callingPid);
// If it's at index 0, it's the current toast. It doesn't matter if it's
// new or just been updated. Call back and tell it to show itself.
// If the callback fails, this will remove it from the list, so don't
// assume that it's valid after this.
if (index == 0) {
showNextToastLocked();
}
} finally {
Binder.restoreCallingIdentity(callingId);
}
}
其中index小于0會新建一個record,否則就會使用隊列里已經(jīng)有的,就是這樣達到優(yōu)化的目的。我加上log運行發(fā)現(xiàn)我的應(yīng)用每次都是-1,普通app就第一次是-1.后面都是0。所以每次都是重新創(chuàng)建的record.
為了盡量少修改源碼,在應(yīng)用中做特殊處理,每次顯示時把原來的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();
}
}
}