如何通过点击的通知来执行的方法通知、方法

2023-09-12 09:26:27 作者:幼儿园扛把子

我有两个按钮的应用程序。一键式的关闭的应用程序,另一个开始的算法。当我点击开始,它隐藏的应用程序,并显示在通知栏的通知。我需要能够执行/调用通知被点击/ pressed时的方法。有几个答案这样的问题,但他们是令人难以置信的模糊和一个只能指向上的BroadcastReceiver的链接文档。

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 doc和说:阅读此页,请不要回答这个问题。如果你要解释,我可以用的BroadcastReceiver来执行的方法(从显示的通知的同一个类中),请告诉我一些$ C $下怎么可以这样做。

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.

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

My algorithm: press a button, display notification, click notification, call a method (don't display activity). That's it.

如果这是不可能的,只是让我知道。如果是,请告诉我你会做什么,使之成为可能。这东西简单不应该被忽视的Andr​​oid SDK的开发者。

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.

推荐答案

通过通知点击请参见我们不能得到任何一旦发生火灾或点击监听器,同时增加在通知栏中的应用程序,我们用它来设置一个悬而未决的意图通常将启动活动中,一旦通知单击或pressed。但我有你的解决方案,如果你真的不想要显示你的活动,然后该活动是要开始挂起的意图送撒施从那里到您的父活动,只是完成挂起的活动,然后一次广播接收器接收任何你想要的接收机内部父活动的呼叫。供大家参考。

See through notification click we cant get any fire event or any click listener, while adding your application in notification bar we use to set a pending intent which usually starts an activity, once notification clicked or pressed. But i have a 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 pressed. 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"));
    }
}