Android 点击通知';不要开始活动
我正在从服务创建通知;通知会显示出来,但当我单击它时,什么也没有发生:它应该打开一个活动 我的代码:Android 点击通知';不要开始活动,android,service,notifications,Android,Service,Notifications,我正在从服务创建通知;通知会显示出来,但当我单击它时,什么也没有发生:它应该打开一个活动 我的代码: NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(icon, "test", when); Intent notificationIn
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, "test", when);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(this, 0,
notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, "title", "message", intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
但是,如果我在活动中使用几乎相同的代码,我可以单击通知,然后显示我的活动。我做错了什么
编辑:
事实证明,该代码没有任何问题,但存在另一个问题:
当我的服务完成时,它用上面的代码创建了通知。但是,该服务还广播它已完成,接收者创建了另一个通知,该通知使用不同的代码创建通知(没有挂起内容,因此在单击通知时没有定义的操作),并且该通知必须放置自身而不是原始通知,正确的一个。这适用于api级别8
private int NOTIFICATION_ID = 1;
private Notification mNotification;
private NotificationManager mNotificationManager;
private PendingIntent mContentIntent;
private CharSequence mContentTitle;
您可以创建如下通知:
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
//create the notification
int icon = R.drawable.ic_launcher;
CharSequence tickerText = mContext.getString(R.string.noti_comes); //Initial text that appears in the status bar
long when = System.currentTimeMillis();
mNotification = new Notification(icon, tickerText, when);
//create the content which is shown in the notification pulldown
mContentTitle = mContext.getString(R.string.noti_comes_t); //Full title of the notification in the pull down
CharSequence contentText = clck_see_noti; //Text of the notification in the pull down
//you can set your click event to go to activity
mContentIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, MainActivity.class), 0);
//add the additional content and intent to the notification
mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);
//make this notification appear in the 'Ongoing events' section
mNotification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL ;
//show the notification
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
不要忘记您的服务正在清单中注册
<service
android:name="com.xx.your_service"
android:enabled="true"
>
</service>
这适用于api级别8
private int NOTIFICATION_ID = 1;
private Notification mNotification;
private NotificationManager mNotificationManager;
private PendingIntent mContentIntent;
private CharSequence mContentTitle;
您可以创建如下通知:
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
//create the notification
int icon = R.drawable.ic_launcher;
CharSequence tickerText = mContext.getString(R.string.noti_comes); //Initial text that appears in the status bar
long when = System.currentTimeMillis();
mNotification = new Notification(icon, tickerText, when);
//create the content which is shown in the notification pulldown
mContentTitle = mContext.getString(R.string.noti_comes_t); //Full title of the notification in the pull down
CharSequence contentText = clck_see_noti; //Text of the notification in the pull down
//you can set your click event to go to activity
mContentIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, MainActivity.class), 0);
//add the additional content and intent to the notification
mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);
//make this notification appear in the 'Ongoing events' section
mNotification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL ;
//show the notification
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
不要忘记您的服务正在清单中注册
<service
android:name="com.xx.your_service"
android:enabled="true"
>
</service>
如果您定义的
活动未注册到AndroidManifest.xml
中,则通知将不会显示任何错误消息,并且不会发生任何情况
Intent notificationIntent = new Intent(this, MainActivity.class);
确保将活动
从注册到AndroidManifest.xml的通知开始
这家伙也有类似的问题:“我在清单上拼错了我的活动名称。”
如果您定义的活动未注册到AndroidManifest.xml
中,则通知将不会显示任何错误消息,并且不会发生任何情况
Intent notificationIntent = new Intent(this, MainActivity.class);
确保将活动
从注册到AndroidManifest.xml的通知开始
这家伙也有类似的问题:“我在清单上拼错了我的活动名称。”
除了使用Android 3.0以上版本的Notification.Builder,或者@Raghunandan在评论中建议的支持库v4中的NotificationCompat.Builder之外,我还遇到了与您的问题可能的通用解决方案相同的问题
这是特定于4.4的,如图所示:
一个已确认的解决方案是对与要创建的PendingEvent相同的PendingEvent执行cancel()操作
对我有效的是修改目标活动的清单定义并添加
android:exported=“true”位于目标活动的“活动”标记内。我想这将是你的主要活动
例如:
<activity
android:name="com.your.MainActivity"
android:exported="true" >
.
.
</activity>
.
.
除了使用适用于以上Android 3.0的Notification.Builder,或者@Raghunandan在评论中建议的支持库v4中的NotificationCompat.Builder之外,我还遇到了与您的问题可能的通用解决方案相同的问题
这是特定于4.4的,如图所示:
一个已确认的解决方案是对与要创建的PendingEvent相同的PendingEvent执行cancel()操作
对我有效的是修改目标活动的清单定义并添加
android:exported=“true”位于目标活动的“活动”标记内。我想这将是你的主要活动
例如:
<activity
android:name="com.your.MainActivity"
android:exported="true" >
.
.
</activity>
.
.
我的案例我的项目使用。所以我的错误是,当我创建意图时,我总是设置:
Intent notificationIntent=新的意图(这是MainActivity.class);
但设置Android注释生成的类时需要:
Intent notificationIntent=新的Intent(这是MainActivity类);
这就解决了我的问题我的案例我的项目使用。所以我的错误是,当我创建意图时,我总是设置:
Intent notificationIntent=新的意图(这是MainActivity.class);
但设置Android注释生成的类时需要:
Intent notificationIntent=新的Intent(这是MainActivity类);
这就解决了我的问题公共通知(int-icon,CharSequence-tickerText,long-when)
此构造函数在API级别11中被弃用。改用Notification.Builder。public Notification(int-icon,CharSequence-tickerText,long-when)
此构造函数在API级别11中被弃用。改用Notification.Builder。