Android的:如何以通知触摸事件作何反应? [英] Android: How to react to notification touch events?

查看:139
本文介绍了Android的:如何以通知触摸事件作何反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我展示通过 NotificationManager 从通知广播接收器,但如果用户触摸通知,什么都没发生。如果我从一个视图中创建的通知,含活性触摸通知时带到前面。

I'm displaying a notification via the NotificationManager from a BroadcastReceiver, but if the user touches the notification, nothing happens. If I create notifications from a view, the containing activity is brought to front when touching the notification.

我怎么能显示特定活动/视图或以其他方式作出反应,用户触摸通知?

How can I display a specific activity/view or otherwise react to the user touching a notification?

推荐答案

当你从NotificationManager接收广播消息,你必须创建一个新的PendingIntent将被解雇时,在适当的通知用户的触摸。现在,必须的PendingIntent有一个内在的意图至极将必须执行,就像启动一个活动的诉讼。

when you receive the broadcast message from the NotificationManager you have to create a new PendingIntent which will be fired when user touch in the proper notification. Now, that PendingIntent must have an inner Intent wich will have an action to perform, like start an activity.

这是在广播接收器调用一个辅助手段你的的onReceive的方法,像showProximityAlertNotification

on your "onReceive" method in the BroadcastReceiver call to an auxiliary method, like "showProximityAlertNotification"

@Override
        public void onReceive(Context context, Intent intent) {
showProximityAlertNotification();
}


//now when the user touch the notification on the notification bar, an activity named //"ItemListActivity" will be launched. You can put an IntentFilter, a Category, an Action 
//to perform any different operations within your activity
private void showProximityAlertNotification(  ){

        String titulo = getString(R.string.notification_proximity_alerts_fired_title);
        String tickerText = getString(R.string.notification_proximity_alerts_fired_ticker);
        String contextText = getString(R.string.notification_proximity_alerts_fired_content);
        int icon = R.drawable.wk_logo;
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);

        //define the actions to perform when user touch the notification
        Intent launchApp = new Intent(getApplicationContext(), ItemListActivity.class);
        launchApp.putExtra("com.xxxxxxx.xxxxxxxxx.bean.Item", "anyObjectYouWant");
        launchApp.setAction( "VIEW_DETAILS_PROPERTY" );
        PendingIntent launchNotification = PendingIntent.getActivity(getApplicationContext(), 0, launchApp, 0);
        notification.setLatestEventInfo(getApplicationContext(), titulo, contextText, launchNotification);

        notificationManager.notify(NOTIFICATION_ID_PROXIMITY_ALERT_FIRED, notification);
    }

如果您正在执行的新推出的活动,并要取消的通知,推出仅仅做到这一点:

if you are executing the newly launched activity and want to cancel the launched notification just do this:

String notManagerName = Context.NOTIFICATION_SERVICE;
                NotificationManager notificationManager = (NotificationManager) getSystemService(notManagerName);
                notificationManager.cancel(ProximityAlerts.NOTIFICATION_ID_PROXIMITY_ALERT_FIRED);

欢呼声

这篇关于Android的:如何以通知触摸事件作何反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆