对于一个apk,做一个ToastService,这样在程序的任何地方都可使用Toast了。然后添加一个public static的api:
public static void toast(String sToast, int duration) {
if (System.currentTimeMillis() - mLastTime < 4000) {
if (sToast.equals(mLastStr)) {
return;
}
}
mLastStr = sToast;
mLastTime = System.currentTimeMillis();
if (mStarted) {
Message msg = mServiceHandler.obtainMessage(EVENT_TOAST);
msg.obj = sToast;
msg.arg1 = duration;
mServiceHandler.sendMessage(msg);
}
else {
Context context = OwlApp.getApplication();
Intent intent = new Intent(context, ToastService.class);
intent.putExtra("toastString", sToast);
intent.putExtra("duration", duration);
context.startService(intent);
}
}