【发布时间】:2022-01-06 10:44:07
【问题描述】:
这是我第一次发帖,如有遗漏,请告知。
我正在开发一个一切正常的前台服务。我的问题是如何从前台服务通知启动应用程序?
一个应用程序关闭,前台服务仍在运行。有没有一种方法可以让用户拉下抽屉并点击正在运行的服务并启动应用程序?
我希望有人可以提供帮助。这是我的服务代码。
private void startForegroundService()
{
try
{
var notifcationManager = GetSystemService(Context.NotificationService) as NotificationManager;
var intent = new Intent(this, typeof(ProteoForegroundService));
PendingIntent pendingIntent = PendingIntent.GetActivity(this, PENDING_INTENT_ID, intent, PendingIntentFlags.OneShot);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
createNotificationChannel(notifcationManager);
}
var notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notification.SetAutoCancel(false);
notification.SetOngoing(true);
notification.SetSmallIcon(Resource.Drawable.Icon);
notification.SetContentTitle("Proteo Mobile");
notification.SetContentText("Proteo Mobile is running");
notification.SetContentIntent(pendingIntent);
StartForeground(NOTIFICATION_ID, notification.Build());
}
catch (Exception ex)
{
Crashes.TrackError(ex, new Dictionary<string, string> { { "ProteoForegroundService.cs", "startForegroundService()" } });
}
}
private void createNotificationChannel(NotificationManager notificationMnaManager)
{
try
{
var channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME,
NotificationImportance.Low);
notificationMnaManager.CreateNotificationChannel(channel);
}
catch (Exception ex)
{
Crashes.TrackError(ex, new Dictionary<string, string> { { "ProteoForegroundService.cs", "createNotificationChannel()" } });
}
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
startForegroundService();
return StartCommandResult.NotSticky;
}
【问题讨论】: