开发者

Custom Notification

开发者 https://www.devze.com 2023-01-04 07:00 出处:网络
I would like to know if there is a way of using the notication bar in order to do some operations (onClick), without having an activity being launched/resumed.

I would like to know if there is a way of using the notication bar in order to do some operations (onClick), without having an activity being launched/resumed.

for example.. let's say i raise a notifcation, and when the user press on it, then instead of take me to some activity, it invoke some regular method 开发者_JS百科in my current activity/service

Is there any way to implement such a thing?

for example the current notifcation code do a standart behave onClick. running up an activity.. how will channge the code in order to invoke some method instead of an activity?

        messagesManager = (NotificationManager) 
               context.getSystemService(Context.NOTIFICATION_SERVICE);


    Notification notification = new Notification(R.drawable.icon, message,
            System.currentTimeMillis());
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, someActivity.class), 0);
    notification.setLatestEventInfo(context, "notification", message,
            contentIntent);
    messagesManager.notify(R.string.noto, notification);


Is there any way to implement such a thing?

Use an appropriate PendingIntent. Instead of calling getActivity(), call getService() or getBroadcast().


I don't believe this is possible or, at the very least, best practice. It's possible that Android could kill your activity while the Notification is still waiting in the top bar. For example maybe you get a phone call but Android is low on RAM - it kills your activity, thus there's really not a 'current' activity anymore.

0

精彩评论

暂无评论...
验证码 换一张
取 消