我想在状态栏中单击通知时打开活动。我在stackoverflow上看到过这个答案,但这些答案对我都不起作用。这是我的代码:

    notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setProgress(100, 0, false);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.drawable.ic_action_file_cloud_upload);
    notificationBuilder.setContentTitle(getString(R.string.notification_upload_title));

    //when this notification is clicked and the upload is running, open the upload fragment
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    // set intent so it does not start a new activity
    PendingIntent intent = PendingIntent.getActivity(this, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    notificationBuilder.setContentIntent(intent);

    this.startForeground(NOTIFICATION_ID_UPLOADING_PROGRESS, notificationBuilder.build());

清单.xml
<activity
        android:name="com.android.app.MainActivity_"
        android:label="@string/app_short_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

最佳答案

最后我意识到我必须写下:

Intent notificationIntent = new Intent(this, MainActivity_.class);

而不是
Intent notificationIntent = new Intent(this, MainActivity.class);

非常感谢你的回答!

08-18 19:58