我从服务调用“ generateNotification”,但单击通知活动后无法打开:
来自服务:generateNotification(getApplicationContext(),contentTitle,contentText,contentText,contentTitle,contentTitle);
服务中的方法:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void createNotificationAPI15(Context context, String title, String message, String ticker_message, String summary_message, String big_message) {
int id = (new Random()).nextInt(150) + 1;
mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, ListActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent contentIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
Notification notif = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.index2)
.setContentIntent(contentIntent)
.setTicker(ticker_message)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(big_message))
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.index2))
.setAutoCancel(true)
.build();
mNotificationManager.notify(id, notif);
}
在清单
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="22" />
....
<activity
android:name=".ListActivity"
android:exported="true"
android:label="@string/list_act_name" >
</activity>
最佳答案
像这样创建您的PendingIntent(对我有用):
Intent intent = new Intent(context, ListActivity.class);
// optional flags, such as:
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
确保您将ListActivity.class传递给intent而不是android.app.ListActivity.class!