我有一个带有两个按钮的应用程序。一个“关闭”应用程序的按钮和一个开始算法的按钮。当我点击“开始”时,它“隐藏”应用程序并在通知栏中显示通知。我需要能够在单击/按下通知时执行/调用方法。这类问题有几个答案,但它们非常模糊,只能指向BroadcastReceiver上文档的链接。
如果您要将一个网址留给BroadcastReceiver文档并说“阅读此页面”,请不要回复此问题。如果您要解释我如何使用BroadcastReceiver来执行一个方法(从显示通知的同一个类中),请向我展示一些如何完成此操作的代码。
我的算法:按下按钮,显示通知,点击通知,调用方法(不显示活动)。就是这样。
如果不可能,请告诉我。如果是,请告诉我你将做些什么来使它成为可能。这个简单的东西不应该被android sdk的开发人员忽视。
答案 0 :(得分:34)
经过多次反复试验,我终于找到了一种相当简单明了的方法,可以在点击通知操作时运行任意方法。在我的解决方案中,有一个类(我称之为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);
}
}
}
现在只需在onHandleIntent中实施您的操作,并将NotificationActionService添加到<application>
标记内的清单中:
<service android:name=".NotificationUtils$NotificationActionService" />
<强>要点:强>
答案 1 :(得分:11)
在通知单击中,我们无法获取任何火灾事件或任何点击监听器。当我们在通知栏中添加通知时,我们可以设置待处理的意图,在通知点击时触发意图(活动/服务/广播)。
我有一个适合你的解决方案,如果你真的不想显示你的活动,那么以待定意图开始的活动会从那里向你的父活动发送一个广泛的演员,然后完成待处理的活动,然后,一旦广播接收器在父活动中接收到你想要的接收器内的任何方法。供您参考..
// 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"));
}
}