Android 如何通过单击通知来执行方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11270898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute a method by clicking a notification
提问by AnDev
I have an application with two buttons. One button that "closes" the application and one that begins the algorithm. When I click "begin" it "hides" the application and displays a notification in the notification bar. I need to be able to execute/call a method when the notification is clicked/pressed. There are a few answers for this sort of question, but they are incredibly vague and one only points to a link to the doc on BroadcastReceiver.
我有一个带有两个按钮的应用程序。一个“关闭”应用程序的按钮和一个启动算法的按钮。当我单击“开始”时,它会“隐藏”应用程序并在通知栏中显示通知。我需要能够在点击/按下通知时执行/调用一个方法。这类问题有几个答案,但它们非常含糊,只有一个指向 BroadcastReceiver 上文档的链接。
If you are going to leave a url to the BroadcastReceiver doc and say "read this page," please don't reply to this question. If you are going to explain how I could use BroadcastReceiver to execute a method (from within the same class that displayed the notification), please show me some code for how this could be done.
如果您要在 BroadcastReceiver 文档中留下 url 并说“阅读此页面”,请不要回复此问题。如果您要解释我如何使用 BroadcastReceiver 执行一个方法(从显示通知的同一个类中),请向我展示一些如何完成的代码。
My algorithm: press a button, display notification, click notification, call a method (don't display activity). That's it.
我的算法:按下按钮,显示通知,点击通知,调用方法(不显示活动)。就是这样。
If it's not possible, just let me know. If it is, please show me what you would do to make it possible. Something this simple shouldn't have been overlooked by the developers of the android sdk.
如果不可能,请告诉我。如果是,请告诉我你会怎么做才能使它成为可能。android sdk 的开发人员不应忽视这么简单的事情。
回答by Tony Wickham
After several iterations of trial and error, I finally found a fairly straightforward and clean way to run an arbitrary method when a notification's action is clicked. In my solution, there is one class (I'll call it NotificationUtils) that creates the notification and also contains an IntentService static inner class that will run when actions on the notification are clicked. Here is my NotificationUtils class, followed by the necessary changes to AndroidManifest.xml:
经过几次反复试验和错误,我终于找到了一种相当简单和干净的方法来在单击通知的操作时运行任意方法。在我的解决方案中,有一个类(我将其称为 NotificationUtils)用于创建通知,并且还包含一个 IntentService 静态内部类,该类将在单击通知上的操作时运行。这是我的 NotificationUtils 类,然后是对 AndroidManifest.xml 的必要更改:
public class NotificationUtils {
public static final int NOTIFICATION_ID = 1;
public static final String ACTION_1 = "action_1";
public static void displayNotification(Context context) {
Intent action1Intent = new Intent(context, NotificationActionService.class)
.setAction(ACTION_1);
PendingIntent action1PendingIntent = PendingIntent.getService(context, 0,
action1Intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Sample Notification")
.setContentText("Notification text goes here")
.addAction(new NotificationCompat.Action(R.drawable.ic_launcher,
"Action 1", action1PendingIntent));
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
public static class NotificationActionService extends IntentService {
public NotificationActionService() {
super(NotificationActionService.class.getSimpleName());
}
@Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
DebugUtils.log("Received notification action: " + action);
if (ACTION_1.equals(action)) {
// TODO: handle action 1.
// If you want to cancel the notification: NotificationManagerCompat.from(this).cancel(NOTIFICATION_ID);
}
}
}
Now just implement your actions in onHandleIntent and add the NotificationActionService to your manifest within the <application>
tags:
现在只需在 onHandleIntent 中实现您的操作,并将 NotificationActionService 添加到您的清单中的<application>
标签中:
<service android:name=".NotificationUtils$NotificationActionService" />
Summary:
概括:
- Create a class that will create the notification.
- Inside that class, add a IntentService inner classes (make sure it is static or you will get a cryptic error!) that can run any method based on the action that was clicked.
- Declare the IntentService class in your manifest.
- 创建一个将创建通知的类。
- 在那个类中,添加一个 IntentService 内部类(确保它是静态的,否则你会得到一个神秘的错误!),它可以根据点击的操作运行任何方法。
- 在清单中声明 IntentService 类。
回答by Daud Arfin
On Notification click we can't get any fire event or any click listener. When we add notification in notification bar, we can set a pending intent, which fires an intent (activity/service/broadcast) upon notification click.
在通知点击时,我们无法获得任何火灾事件或任何点击侦听器。当我们在通知栏中添加通知时,我们可以设置一个挂起的意图,它会在点击通知时触发一个意图(活动/服务/广播)。
I have a workound solution for you, if you really don't want to display your activity then the activity which is going to start with pending intent send a broad cast from there to your parent activity and just finish the pending activity and then once broadcast receiver receives in parent activity call whatever method you want inside the receiver. For your reference..
我为您提供了一个可行的解决方案,如果您真的不想显示您的活动,那么将以挂起意图开始的活动从那里向您的父活动发送广播,然后完成挂起的活动,然后进行一次广播接收器在父活动调用中接收您想要在接收器中使用的任何方法。供你参考..
// This is what you are going to set a pending intent which will start once
// notification is clicked. Hopes you know how to add notification bar.
Intent notificationIntent = new Intent(this, dummy_activity.class);
notificationIntent.setAction("android.intent.action.MAIN");
notificationIntent.addCategory("android.intent.category.LAUNCHER");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT |
Notification.FLAG_AUTO_CANCEL);
// Now, once this dummy activity starts send a broad cast to your parent activity and finish the pending activity
//(remember you need to register your broadcast action here to receive).
BroadcastReceiver call_method = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action_name = intent.getAction();
if (action_name.equals("call_method")) {
// call your method here and do what ever you want.
}
};
};
registerReceiver(call_method, new IntentFilter("call_method"));
}
}