Android 如何通过单击通知来执行方法

Android 如何通过单击通知来执行方法,android,notifications,Android,Notifications,我有一个有两个按钮的应用程序。一个按钮“关闭”应用程序,另一个按钮开始算法。当我单击“开始”时,它“隐藏”应用程序并在通知栏中显示通知。我需要能够在单击/按下通知时执行/调用方法。对于这类问题有一些答案,但它们都非常模糊,只有一个指向BroadcastReceiver上文档的链接 如果你打算留下一个指向BroadcastReceiver文档的url并说“阅读此页面”,请不要回答此问题。如果您要解释如何使用BroadcastReceiver执行方法(在显示通知的同一类中),请向我展示一些代码,说明

我有一个有两个按钮的应用程序。一个按钮“关闭”应用程序,另一个按钮开始算法。当我单击“开始”时,它“隐藏”应用程序并在通知栏中显示通知。我需要能够在单击/按下通知时执行/调用方法。对于这类问题有一些答案,但它们都非常模糊,只有一个指向BroadcastReceiver上文档的链接

如果你打算留下一个指向BroadcastReceiver文档的url并说“阅读此页面”,请不要回答此问题。如果您要解释如何使用BroadcastReceiver执行方法(在显示通知的同一类中),请向我展示一些代码,说明如何执行

我的算法:按下按钮,显示通知,单击通知,调用方法(不显示活动)。就这样


如果不可能,就告诉我。如果是的话,请告诉我你会怎么做,使之成为可能。android sdk的开发人员不应该忽视这么简单的事情。

在通知单击时,我们无法获得任何fire事件或任何单击侦听器。当我们在通知栏中添加通知时,我们可以设置一个挂起的意图,在单击通知时触发一个意图(活动/服务/广播)

我有一个健身方案给你,如果您确实不想显示您的活动,那么将以挂起意图开始的活动将从那里向您的父活动发送一个广泛的广播,并仅完成挂起的活动,然后一旦广播接收器在父活动中接收到,在接收器内调用您想要的任何方法。供你参考

// 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"));
    }
}

经过几次反复的尝试和错误之后,我终于找到了一种非常简单明了的方法,可以在单击通知的操作时运行任意方法。在我的解决方案中,有一个类(我称之为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);
            }
        }
}
现在只需在OnHandleContent中实现您的操作,并将NotificationActionService添加到
标记中的清单中:

<service android:name=".NotificationUtils$NotificationActionService" />

摘要:

  • 创建将创建通知的类
  • 在该类中,添加一个IntentService内部类(确保它是静态的,否则会出现隐藏的错误!),该类可以基于单击的操作运行任何方法
  • 在清单中声明IntentService类

感谢您的回复。这是非常彻底和有用的。如果我想在通知点击事件中检查应用程序是否在前台,并且根据您的代码@Daud Arfin,如果我们启动活动,它将始终返回应用程序在前台。是否有其他方法可以检查应用程序是否在前台,或者是否在通知单击时不在前台event@ErKimmi Dhingra-你可以依赖android的生命周期。。在onPause/onStop和onResume/onStart中维护标志。在本例中,notificationIntent.setAction和.addCategory实现了什么?我只让通知管理器通知并在通知栏中显示通知。当我单击通知时,它将打开活动。我想通过调用/执行方法而不是打开活动来修改此行为。我正试着用Arfin的解决方案。我只是不知道我该怎么做。我创建了一个“DummyActivity”,我不确定他的解决方案的第二部分是否应该在“DummyActivity”中。我只是感到困惑。我喜欢事情顺利地结合在一起。是的,AnDev一旦你的虚拟活动开始,就从他们的活动发送一个广播并完成它,所以现在你将在父类中接收广播消息,正如我已经解释过的,你可以从那里调用任何你想要的方法。除了发送广播并完成它之外,不需要在虚拟活动中做任何进一步的事情。如果操作字段已经在使用,该怎么办?像“ACTION_VIEW”一样,您可以在意图中添加额外的内容,例如Action1Content.putExtra(“action1extra”,1),然后在onHandleIntent中使用,如果(Intent.getIntExtra(“action1extra”),1)==1{…}到目前为止,我花了一天的大部分时间来寻找实现这一点的方法。很高兴我找到了你的答案,很有魅力。