【问题标题】:android How to open last activity when tapping notificationandroid如何在点击通知时打开最后一个活动
【发布时间】:2015-07-16 08:15:54
【问题描述】:

我观察到了几个类似的问题,但他们无法帮助我。当用户单击通知时,我需要显示最后一个活动。这是我的代码:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(YourService.this)
        .setContentTitle(getResources().getText(R.string.app_name))
        .setContentText(getServiceStateDescription(YourService.this))
        .setSmallIcon(iconId)
        .setWhen(System.currentTimeMillis());

Intent nIntent = getPreviousIntent();
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(MainActivity_.class);
stackBuilder.addNextIntent(nIntent);
PendingIntent pendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);

startForeground(ContextConstants.LAUNCHER_SERVICE_NOTE_ID, notificationBuilder.build());


private Intent getPreviousIntent() {
Intent newIntent = null;
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    final List<ActivityManager.AppTask> recentTaskInfos = activityManager.getAppTasks();
    if (!recentTaskInfos.isEmpty()) {
        for (ActivityManager.AppTask appTaskTaskInfo: recentTaskInfos) {
            if (appTaskTaskInfo.getTaskInfo().baseIntent.getComponent().getPackageName().equals(ContextConstants.PACKAGE_NAME)) {
                newIntent = appTaskTaskInfo.getTaskInfo().baseIntent;
                newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
        }
    }
} else {
    final List<ActivityManager.RecentTaskInfo> recentTaskInfos = activityManager.getRecentTasks(1024, 0);
    if (!recentTaskInfos.isEmpty()) {
        for (ActivityManager.RecentTaskInfo recentTaskInfo: recentTaskInfos) {
            if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(ContextConstants.PACKAGE_NAME)) {
                newIntent = recentTaskInfo.baseIntent;
                newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
        }
    }
}
if (newIntent == null) newIntent = new Intent();
return newIntent;

}

第二点是,如果我使用不推荐使用的方法来构建通知,显示最后一个活动效果很好,因为我想要这里是代码:

Notification note = new Notification(iconId,
            getResources().getText(R.string.serviceStarted),
            System.currentTimeMillis());
    note.flags |= Notification.FLAG_ONGOING_EVENT;
    note.flags |= Notification.FLAG_NO_CLEAR;

    Intent nIntent = new Intent();
    nIntent = getPreviousIntent(nIntent);
    PendingIntent pi = PendingIntent.getActivity(this, 0, nIntent, 0);
    note.setLatestEventInfo(this,
            getResources().getText(R.string.app_name), getServiceStateDescription(YourService.this), pi);

    startForeground(ContextConstants.LAUNCHER_SERVICE_NOTE_ID, note);

But i don't want use deprecated metods anymore, that's why i'm here explaining. Thanks in advance!

我也见过this doc。

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>

<permission android:name="com.my.package.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.my.package.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.GET_TASKS"/>

<application
    android:name=".MyApplication"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light">
    <activity android:name=".activity.StartingActivity_"  android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".activity.MainActivity_"
        android:configChanges="orientation|screenSize">
    </activity>
    <activity
        android:name=".activity.ComposeEmailActivity_"
        android:configChanges="orientation|screenSize"
        android:parentActivityName=".activity.MainActivity_">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activity.MainActivity_" />
    </activity>
    <activity
        android:name=".activity.EditAccountActivity_"
        android:configChanges="orientation|screenSize"
        android:parentActivityName=".activity.MainActivity_">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activity.MainActivity_" />
    </activity>               

    <!--android:process=":remote" is for working in background-->
    <service android:enabled="true" android:name=".service.YourService"/>       

</application>

【问题讨论】:

  • 您是否只想将您的应用程序以它进入后台时的任何状态带到前台?

标签: android notifications android-pendingintent


【解决方案1】:

您的问题不在于您用于构建Notification 的方法。你的问题是你使用TaskStackBuilder。使用这个类在幕后执行了很多隐藏的魔法,并且它对您希望应用程序的行为做出了很多假设。不幸的是,这些假设大多是错误的:-(

如果你只是想把你的应用程序以它进入后台时的任何状态带到前台,你不想使用TaskStakBuilder,你也不必使用所有那些讨厌的@ 987654325@ 方法。只需这样做:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(YourService.this)
    .setContentTitle(getResources().getText(R.string.app_name))
    .setContentText(getServiceStateDescription(YourService.this))
    .setSmallIcon(iconId)
    .setWhen(System.currentTimeMillis());

Intent nIntent = getPackageManager().
        getLaunchIntentForPackage(ContextConstants.PACKAGE_NAME);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, nIntent,
    PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);

startForeground(ContextConstants.LAUNCHER_SERVICE_NOTE_ID,
    notificationBuilder.build());

【讨论】:

  • 嗨!感谢 @David Wasser 的快速回复,但它总是会打开启动器活动。
  • 另外,您是如何第一次启动该应用程序的?从安装程序或 IDE 或通过单击主屏幕上的应用程序图标?
  • 您可能会看到一个令人讨厌的 Android 错误。请尝试以下操作: 不要从 Android Studio 启动您的应用程序。只需在手机(或模拟器)上部署应用程序,然后通过点击主屏幕上的应用程序图标手动启动它。然后看看你的通知是否总是打开启动器 Activity。
  • 很高兴为您提供帮助。有关该错误的更多详细信息,请参阅stackoverflow.com/a/16447508/769265
  • 我们如何恢复现有的应用程序?通过上面的代码,每次新应用程序甚至应用程序在后台时都会启动。
猜你喜欢
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-19
相关资源
最近更新 更多