Android 如何使用任何通知消息启动活动,例如;abc";

Android 如何使用任何通知消息启动活动,例如;abc";,android,broadcastreceiver,notificationmanager,Android,Broadcastreceiver,Notificationmanager,当我收到通知信息“abc”时,我想打开我的应用程序。我可以用SMSReceiver来做这件事,但只能收到短信。我想做这个whatsapp消息。很抱歉英语不好 @Override public void onReceive(Context context, Intent intent) { String hangiNumaradan = ""; String neYazmis = ""; Bundle bundle = intent.getExtras(); S

当我收到通知信息“abc”时,我想打开我的应用程序。我可以用SMSReceiver来做这件事,但只能收到短信。我想做这个whatsapp消息。很抱歉英语不好

@Override
public void onReceive(Context context, Intent intent) {

    String hangiNumaradan = "";
    String neYazmis = "";

    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;


    if (bundle != null) {

        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            hangiNumaradan += msgs[i].getOriginatingAddress();
            neYazmis += msgs[i].getMessageBody().toString();
        }


        Toast.makeText(context, hangiNumaradan + " gelen mesaj " + neYazmis, Toast.LENGTH_LONG).show();
    }

}
@覆盖
公共void onReceive(上下文、意图){
字符串hangiNumaradan=“”;
字符串neYazmis=“”;
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
if(bundle!=null){
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];
对于(int i=0;i

这段代码是smsreceiver和toast message。

在做了一些调查后,我意识到自从Facebook收购Whatsapp以来,他们关闭了所有公开的Whatsapp API,现在不可能创建Whatsapp消息接收器,但有一件事你可以做

  • 创建可访问性服务

    公共类MyAccessibilityService扩展了AccessibilityService{

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        //here you can implement your reaction for incoming notification
        //here you can find some tags that could be helpful for you
        String helpful = event.getContentDescription();
    
        //In code below you could also retrieve some helpful info from Notification
        AccessibilityNodeInfo source = event.getSource();
            if (source == null) {
                return;
            }
    
            // Grab the parent of the view that fired the event.
            AccessibilityNodeInfo rowNode = getListItemNodeInfo(source);
            if (rowNode == null) {
                return;
            }
    
            // Using this parent, get references to both child nodes, the label and the checkbox.
            AccessibilityNodeInfo labelNode = rowNode.getChild(0);
            if (labelNode == null) {
                rowNode.recycle();
                return;
            }
    
            AccessibilityNodeInfo completeNode = rowNode.getChild(1);
            if (completeNode == null) {
                rowNode.recycle();
                return;
            }
    
            // Determine what the task is and whether or not it's complete, based on
            // the text inside the label, and the state of the check-box.
            if (rowNode.getChildCount() < 2 || !rowNode.getChild(1).isCheckable()) {
                rowNode.recycle();
                return;
            }
    
            CharSequence anotherInfoFromNotification = labelNode.getText();
    
    }
    
    @Override
    public void onInterrupt() {
    }
    
    @覆盖
    AccessibilityEvent上的公共无效(AccessibilityEvent事件){
    //在这里,您可以实现对传入通知的反应
    //在这里你可以找到一些对你有帮助的标签
    字符串help=event.getContentDescription();
    //在下面的代码中,您还可以从通知中检索一些有用的信息
    AccessibilityNodeInfo source=event.getSource();
    if(source==null){
    回来
    }
    //获取触发事件的视图的父级。
    AccessibilityNodeInfo行节点=getListItemNodeInfo(源);
    if(rowNode==null){
    回来
    }
    //使用此父节点,获取对两个子节点、标签和复选框的引用。
    AccessibilityNodeInfo labelNode=rowNode.getChild(0);
    if(labelNode==null){
    rowNode.recycle();
    回来
    }
    AccessibilityNodeInfo completeNode=rowNode.getChild(1);
    if(completeNode==null){
    rowNode.recycle();
    回来
    }
    //根据以下内容确定任务是什么以及任务是否完成
    //标签内的文本以及复选框的状态。
    if(rowNode.getChildCount()<2 | |!rowNode.getChild(1.isCheckable()){
    rowNode.recycle();
    回来
    }
    CharSequence AnotherInfo fromNotification=labelNode.getText();
    }
    @凌驾
    在中断时的公共无效(){
    }
    
    }

  • 创建
    serviceconfig.xml
    文件:

  • 在清单中注册服务:

    
    


  • 总而言之:您将能够让侦听器接收到所有传入的whatsapp消息,您必须使用
    onAccessibilityEvent
    方法中的代码进行更多尝试,以从消息中检索信息。

    如果我清楚地理解您的意思,那么当您单击通知栏中的通知时,您希望打开活动,对吧?你不对。我的手机收到通知后会自动启动我的程序。例如手机在通知中接收whatsapp消息,自动启动我的活动。谢谢你的评论好的,但是我仍然不知道你想要实现什么,如果你提供多一点信息,那么也许我可以帮助你。好的:)你给我发送了whatsapp消息“兄弟”我的手机外观通知,如果你的whatsapp消息是兄弟,打开我的活动并在edittext中写下“兄弟”。你明白了吗?A明白了,5分钟后读答案。