[英]How to check if application is running on background?
我正在使用 firebase 向應用程序發送通知,並根據通知將用戶定向到某些活動。 到目前為止一切順利,我遇到的問題是,如果應用程序在后台運行,則將顯示的活動是主要活動,但我無法完全設法做到這一點。
我相信有幾個這樣的問題,但並沒有得到我正在尋找的答案。
任何幫助或建議都會很棒,謝謝
Android 10(API 級別 29)和更高版本對應用在后台運行時何時可以啟動 Activity 設置了限制。 這些限制有助於最大限度地減少對用戶的干擾,並使用戶能夠更好地控制屏幕上顯示的內容。
嘗試
/**Flag to determine if the Activity is visible*/
private static boolean activityVisible;
private static boolean activityDestroy;
/**Is the application actually visible on the mobile screen*/
public static boolean isActivityVisible(){
return activityVisible;
}
/**Is the application actually destroyed*/
public static boolean isActivityDestroy(){
return activityDestroy;
}
/**Is the application actually destroyed*/
public static void activityDestroy(boolean isDestroy){
activityDestroy = isDestroy;
}
/**Is the application actually in the background*/
public static boolean isActivityInBackground(){
return !activityVisible;
}
/**Change the state of the Application to resume*/
public static void activityResumed() {
activityVisible = true;
}
/**Change the state of the Application to paused*/
public static void activityPaused() {
activityVisible = false;
}
然后檢查
Application app = ((Application)getApplicationContext());
boolean visible = app.isActivityVisible();
您可以使用ActivityManager 的重要字段。 下面是 C# 使用的 Xamarin 方法,但它與 Java 非常相似。
PS:如果你願意,我可以將該代碼移植到 Java
public static Importance GetAppState(Context context, string packageName)
{
try
{
var manager = (ActivityManager)context.GetSystemService(Context.ActivityService);
var processes = manager.RunningAppProcesses;
if (processes != null)
foreach (var process in processes)
if (process.ProcessName.Equals(packageName))
return process.Importance;
}
catch
{
}
return Importance.Gone;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.