简体   繁体   English

如何将现有活动从服务带回前台?

[英]How do I bring an existing activity back to the front from a service?

I have a background service which is running and I am using a notification and a PendingIntent to allow the user to "reopen" the existing home activiy. 我有一个正在运行的后台服务,并且正在使用通知和PendingIntent,以允许用户“重新打开”现有的家庭活动。 Unfortunately what's happening is that it's creating a new home activity stacked on top of the one I want to go to meaning that the user has to hit the back button to get back to the original one. 不幸的是,发生的事情是它正在创建一个新的家庭活动,该活动堆叠在我要转到的活动之上,这意味着用户必须单击“后退”按钮才能返回到原始活动。 What do I have to change in the following service code to ensure it simply reopens the existing Activity? 我必须在以下服务代码中进行哪些更改,以确保它可以简单地重新打开现有的活动?

    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);

My manifest: 我的清单:

<?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>

you might give your activity a "single top" flag, and handle relaunch stuff in your OnNewIntent. 您可以为活动指定“顶部”标志,并处理OnNewIntent中重新启动的内容。 You can refer to http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent) 您可以参考http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)

I found an answer though I can't explain why this works or what the difference is. 我找到了一个答案,尽管我无法解释为什么这样做或有什么区别。 If anyone would care to elaborate on what this changes, I'd appreciate actually understanding it. 如果有人愿意详细说明这种变化,我将不胜感激。

Adding the following lines manually to my manifest file solved the problem: 将以下几行手动添加到清单文件即可解决此问题:

<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>

It was suggested here in an answer which was never accepted: 这里提出了一个从未被接受的答案:

How to bring Android existing activity to front via notification 如何通过通知将Android现有活动放在首位

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM