在挂起的意图中添加堆栈

时间:2015-12-05 15:35:52

标签: java android android-intent

我试图在我的广播接收器的待定意图上添加一个后台,但不幸的是,当我从通知中打开活动并单击工具栏中的箭头时它不起作用,它只是使我的应用程序关闭。

这是我的广播接收器:

public class AlertReceiver extends BroadcastReceiver {

    private int id;
    // Called when a broadcast is made targeting this class
    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        String title = bundle.getString("title");
        String time = bundle.getString("time");
        id = bundle.getInt("id");

        createNotification(context, title, time, "Pharoah Reminder");
    }

    public void createNotification(Context context, String msg, String msgText,  String msgAlert){
        Intent reminderActivity =  new Intent(context, ReminderPreviewActivity.class);
        reminderActivity.putExtra("id", id);
        reminderActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        // All the parents of SecondActivity will be added to task stack.
        stackBuilder.addParentStack(MainActivity.class);
        // Add a SecondActivity intent to the task stack.
        stackBuilder.addNextIntent(reminderActivity);

        PendingIntent pendingIntent = stackBuilder.getPendingIntent(id, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(msg)
                .setTicker(msgAlert)
                .setContentText(msgText)
                .setContentIntent(pendingIntent);

        mBuilder.setContentIntent(pendingIntent);
        mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);

        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(id, mBuilder.build());
    }
}

清单文件:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".activities.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver android:name=".broadcastReceiver.AlertReceiver" />

        <activity
            android:name=".activities.AddReminderActivity"
            android:label="@string/title_activity_add_reminder" >
        </activity>
        <activity
            android:name=".activities.ReminderPreviewActivity"
            android:label="@string/title_activity_reminder_preview"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".activities.MainActivity"/>
        </activity>
    </application>

1 个答案:

答案 0 :(得分:-2)

您可以通过以下方式自定义背压行为:

@Override 
public void onBackPressed() { 
   //...
}