在设置通知

时间:2016-08-30 09:42:31

标签: android

我想在当前Activity开始通知ChatActivity。当我按下通知时,我想输入ChatOneActivity

但是当我在ChatActivity时,我不希望当前ChatOneActivity完成。因为我收到了数据,当我按下后退按钮时,我想留在ChatActivity

关键是我不希望ChatActivity完成,无论我目前在哪Activity

那我该怎么办?

这是代码

private void showNotification(String id, String message) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
    .setSmallIcon(android.R.drawable.sym_action_email)
    .setContentTitle("You have a new message")
    .setContentText(message);
    Intent intent = new Intent(ChatActivity.this,ChatOneActivity.class);
    intent.putExtra("toId", id);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(ChatActivity.this);
    stackBuilder.addParentStack(ChatOneActivity.class);
    stackBuilder.addNextIntent(intent);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setContentIntent(pendingIntent);
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, builder.build());
}

问题是:

现在我输入ChatOneActivity,当我按下后退按钮时,我会返回桌面。意味着ChatActivity已经完成了我不想要的。

这是主要的节日

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webmobilegroupchat"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.webmobilegroupchat.MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.example.webmobilegroupchat.ChatActivity"
        android:label="@string/title_activity_chat" >
    </activity>
    <activity
        android:name="com.example.webmobilegroupchat.ChatOneActivity"
        android:label="@string/title_activity_chat_one" >
    </activity>
    <activity
        android:name="com.example.webmobilegroupchat.SplashActivity"
        android:label="@string/title_activity_splash" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

2 个答案:

答案 0 :(得分:1)

在ChatOneActivity活动中尝试此操作。

  @Override
  public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(getApplicationContext(),ChatActivity.Class);
    startActivity(intent);
}

答案 1 :(得分:0)

您的代码看起来很适合创建通知,但您必须在Manifest中定义父活动以导航回来,stackBuilder.addParentStack假定您在Manifest中定义它以及API 16 +:

<activity android:name=".ChatOneActivity"
    android:parentActivityName=".ChatActivity"
    ... />

for&lt; API 16:

<activity android:name=".ChatOneActivity">
<meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value=".ChatActivity" />