【发布时间】:2021-11-21 11:08:37
【问题描述】:
我的应用程序 (Xamarin.Android) 作为前台服务运行。因此,该服务有一个我更新的永久通知。该应用程序从支持蓝牙的医疗设备接收数据。当通讯到达我的应用程序时,我会使用计数器更新通知(在我的例子中是患者事件)。
如果我点击通知,我的应用程序在那里启动得很好,但是,对于某些传入的蓝牙数据包,我需要实际启动(或前台)我的活动,这需要在用户不点击通知的情况下发生。注意:我只希望这在设备解锁、屏幕打开且我的应用不在前台时才能工作。
我的代码过去运行得很好,所以我怀疑它的谷歌对 Android 10 和 11 的更改已经停止了这个工作,但它仍然可以完成吗?
我当前的代码如下所示
非常感谢
卡伦
/// <summary>
/// Assuming that the phone is not locked, and the screen is on, this brings the application to the foreground. It is used, for instance
/// where a patient event is inititiated while the user is viewing another app. The app is brought to the foreground by simply launching (or re-launching)
/// Main Activity
/// </summary>
public void BringToForeground()
{
var context = (Activity)MainApplication.ActivityContext;
KeyguardManager keyguardManager = (KeyguardManager)context.GetSystemService(Context.KeyguardService);
DisplayManager displayManager = (DisplayManager)context.GetSystemService(Context.DisplayService);
var displayOn = false;
foreach (var display in displayManager.GetDisplays())
{
if (display.State == DisplayState.On)
displayOn = true;
}
if (!displayOn || keyguardManager.IsKeyguardLocked)
return;
//Check if we are already foregrounded, if so, return, nothing more to do
var proteusAppProcess = new ActivityManager.RunningAppProcessInfo();
ActivityManager.GetMyMemoryState(proteusAppProcess);
if (proteusAppProcess.Importance == Importance.Foreground)
return;
//Not foregrounded so re-launch intent - since this APP is SingleTop, this will replace any existing activity
Intent resultIntent = new Intent(StaticDefs.Com_Spacelabs_EclipsePatientApp_Android_SwitchScreenIntent);
resultIntent.PutExtra(PageId.PageIdStringIdent, (int)PageId.RequestedPageId.PatientEventListScreen);
resultIntent.SetFlags(ActivityFlags.NoHistory | ActivityFlags.NewTask | ActivityFlags.SingleTop);
context.StartActivity(resultIntent);
}
【问题讨论】:
标签: android android-activity notifications