【发布时间】:2016-05-30 10:24:12
【问题描述】:
当我单击状态栏中的通知时,我想打开一个活动。我在 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());
Manifest.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>
【问题讨论】:
-
你为什么用
this.startForeground而不是NotificationManager.notify? -
点击通知会发生什么?
-
@Malith Lakshan,状态栏已关闭,没有任何反应。
-
检查提供给待处理意图的上下文是否正确,尝试使用 ClassName.this,如果它来自服务或活动
标签: android android-notifications