[英]How do I bring an existing activity back to the front from a service?
我有一個正在運行的后台服務,並且正在使用通知和PendingIntent,以允許用戶“重新打開”現有的家庭活動。 不幸的是,發生的事情是它正在創建一個新的家庭活動,該活動堆疊在我要轉到的活動之上,這意味着用戶必須單擊“后退”按鈕才能返回到原始活動。 我必須在以下服務代碼中進行哪些更改,以確保它可以簡單地重新打開現有的活動?
Intent notificationIntent = new Intent(ApplicationContext, typeof( MainActivity ) );
notificationIntent.SetFlags (ActivityFlags.NewTask);
PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, 0 );
Notification notification = new Notification (Resource.Drawable.Icon, "Playing: " + mStreamName );
notification.Flags |= NotificationFlags.OngoingEvent;
notification.SetLatestEventInfo (this, "MyApp", "Announcement!", pendingIntent);
StartForeground((int)NotificationFlags.ForegroundService, notification);
我的清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="..." android:versionCode="2" android:versionName="2.0">
<uses-sdk />
<application android:label="..." android:icon="@drawable/icon">
<service android:enabled="true" android:name=".AudioService"/>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>
您可以為活動指定“頂部”標志,並處理OnNewIntent中重新啟動的內容。 您可以參考http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)
我找到了一個答案,盡管我無法解釋為什么這樣做或有什么區別。 如果有人願意詳細說明這種變化,我將不勝感激。
將以下幾行手動添加到清單文件即可解決此問題:
<activity android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
這里提出了一個從未被接受的答案:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.