這是我第一次發帖,所以如果我錯過了什么,請告訴我。
我正在開發一個一切正常的前臺服務。我的問題是如何從前臺服務通知啟動應用程式?
應用程式關閉后,前臺服務仍在運行。有沒有辦法允許用戶拉下抽屜并點擊正在運行的服務并啟動應用程式?
我希望有人能幫幫忙。這是我的服務代碼。
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;
}
uj5u.com熱心網友回復:
Notification.SetContentIntent 方法是設定用戶點擊正在運行的服務時要執行的操作的特定方法,并且 PendingIntent.GetActivity 方法應該獲取一個活動。如:
private void startForegroundService()
{
try
{
var notifcationManager = GetSystemService(Context.NotificationService) as NotificationManager;
var intent = new Intent(this, typeof(MainActivity));
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()" } });
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371236.html
上一篇:如何將android專案從VisualStudio部署到googleplay控制臺?
下一篇:Xamarin5.0創建啟影片面
