[英]how to show pop up or custom toast in onStop() or onDestroy()?
最簡單的方法是使用Toast(簡單又直接)。 更復雜的方式是,您必須在之后授予2個特殊權限“在其他應用上繪圖”和“具有使用權限的應用”(這是特殊權限,因此您需要創建自己的頁面以提示用戶授予它)您可以將自定義視圖直接添加到系統的Windows管理器中。 最后但並非最不重要的一點是,您應該認真考慮在主頁被停止或銷毀后在主屏幕上通知用戶是否有任何好處,因為這與系統設計大相徑庭。
如果您只想發送消息,那么Toast
就足夠了而且很容易。
@override
public void onStop() {
Toast.MakeText(context, "Your message here", Toast.LENGTH_LONG).show();
}
@override
public void onDestroy() {
Toast.MakeText(context, "Your message here", Toast.LENGTH_LONG).show();
}
編輯 :我不建議您在Toast
寫長消息並將其顯示更長的時間,而是考慮使用Statusbar Notification 。 當狀態欄通知不再相關時,可以通過編程方式取消它們。
但是,如果您仍然想增加Toast消息的持續時間,則可以采用以下解決方法。
private Toast mToastToShow;
public void showToast(View view) {
// Set the toast and duration
int toastDurationInMilliSeconds = 10000;
mToastToShow = Toast.makeText(this, "Hello world, I am a toast.", Toast.LENGTH_LONG);
// Set the countdown to display the toast
CountDownTimer toastCountDown;
toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, 1000 /*Tick duration*/) {
public void onTick(long millisUntilFinished) {
mToastToShow.show();
}
public void onFinish() {
mToastToShow.cancel();
}
};
// Show the toast and starts the countdown
mToastToShow.show();
toastCountDown.start();
}
它是這樣工作的:倒計時的通知時間比根據標志顯示吐司的持續時間短,因此,如果倒計時尚未結束,則可以再次顯示吐司。 如果在屏幕上仍顯示該吐司,它將在整個過程中保持不變而不會閃爍。 倒計時完成后,即使吐司的顯示時間尚未結束,吐司也將被隱藏起來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.