【问题标题】:Resume activity on notification click [duplicate]通知点击恢复活动[重复]
【发布时间】:2015-03-09 10:28:20
【问题描述】:

我在单击通知时恢复活动时遇到问题。我有一个应用程序可以播放一首歌曲一段时间。这个想法是,当您播放歌曲并按下 Home 按钮时,应该会有通知让您返回到可以停止歌曲的应用程序。
以下是我发出通知的方式:

private void pushNotificationsOld()
{
    Intent resultIntent = new Intent(context, MainActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager mNM;
    NotificationCompat.Builder builder;
    mNM = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(getResources().getString(R.string.app_name))
            .setContentText("Now playing: " + songList.get(activeSong).name)
            .setOngoing(true);
    builder.setContentIntent(resultPendingIntent);
    mNM.notify(mId, builder.build());
}

这是我的清单:

<activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
                android:launchMode="singleInstance">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
 </activity>

pushNotificationsOld() 通过按播放按钮调用。
并且应用程序中只有一项活动。
使用此代码,当我按下通知时,会创建新活动,并且音乐会继续在后台播放。
当我按 Home 并从 recent appsicon 回到应用程序时,效果很好。

【问题讨论】:

标签: android android-activity notifications android-mediaplayer


【解决方案1】:

在您的 manifest.xml 中,添加 MainActivity:

android:launchMode="singleTop"

【讨论】:

  • 我也尝试过,但结果相同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多