gpt4 book ai didi

android - 单击通知时启动 Activity

转载 作者:搜寻专家 更新时间:2023-11-01 08:34:57 25 4
gpt4 key购买 nike

我想在单击状态栏中的通知时打开一个 Activity 。我在 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());

list .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);

非常感谢您的所有回答!

关于android - 单击通知时启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37523303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com