我的应用程序(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);
}
发布于 2021-09-29 06:42:32
是的,我想我可以回答我自己的问题--这是由于Android 10 re的变化,谁可以编程启动前台活动。
要让上面的代码正常工作,我需要请求SYSTEM_ALERT_WINDOW权限,然后手动访问(或以编程方式打开)我的应用程序的安卓设置页面,并启用“显示在顶部”选项。
这样做之后,我的活动就像以前一样开始了。
不幸的是,考虑到这是一个医疗应用程序,可能是老年人使用的,期望人们手动重新配置该应用程序是不现实的,所以我将接受通知tap来启动活动。似乎需要SYSTEM_ALERT_WINDOW权限,即使我的应用程序作为前台服务运行-前台服务是绕过Android9后台执行限制的一种方式!
凯伦
https://stackoverflow.com/questions/69378139
复制