简体   繁体   中英

Android: How to send a message back to my Activity from a Notification, without opening Activity?

In Android, I have a notification and I want to send a message back to the activity (and use the message to control my app). What is the simplest method to do that with PendingIntent ? Is getBroadcast the appropriate way? getActivity opens the activity and I don't want to open it, but only tell it to do something.

您可以创建一个后台服务来接收PendingIntent.getService发送的消息, 是有关创建后台服务的官方指南

you can use EventBus

In 3 steps

Define events:
public class MessageEvent { /* Additional fields if needed */ }

Prepare subscribers:
eventBus.register(this);
public void onEvent(AnyEventType event) {/* Do something */};

Post events:
eventBus.post(event);

or you can use otto

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM