Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从registerReceiver(服务)以字符串形式插入数据?安卓_Android_Android Intent_Android Service_Android Broadcast - Fatal编程技术网

Android 如何从registerReceiver(服务)以字符串形式插入数据?安卓

Android 如何从registerReceiver(服务)以字符串形式插入数据?安卓,android,android-intent,android-service,android-broadcast,Android,Android Intent,Android Service,Android Broadcast,如何从registerReceiver在字符串中插入数据,我有以下代码 在我的服务中我编写了以下代码: Intent ir=new Intent(); ir.setAction(MY_ACTION); ir.putExtra("Webresponse", Webresponse); sendBroadcast(ir); 在活动中我得到数据: private class MyReceiver extends BroadcastR

如何从
registerReceiver
字符串中插入数据,我有以下代码

在我的
服务中
我编写了以下代码:

        Intent ir=new Intent(); 
        ir.setAction(MY_ACTION);
        ir.putExtra("Webresponse", Webresponse); 
        sendBroadcast(ir);
活动中
我得到数据:

private class MyReceiver extends BroadcastReceiver{


         @Override
         public void onReceive(Context arg0, Intent arg1) {
          String datapassed = arg1.getStringExtra("Webresponse");
          Toast.makeText(ChatPage.this,
                "Triggered by Service!\n"
                + "Data passed: " + datapassed,
                Toast.LENGTH_LONG).show();
         }
    }
然后在
onCreate
中,我可以使用
Toast
获取数据并查看数据,但我需要
将数据插入字符串值中:

MyReceiver m = new  MyReceiver();
                            IntentFilter intentFilter = new IntentFilter();
                            intentFilter.addAction(ChatPage_Service.MY_ACTION);
                            registerReceiver(m, intentFilter);
我需要:

String values = registerReceiver(m, intentFilter);

如果要填充列表视图,则应将其填充到您的
私有类MyReceiver extends BroadcastReceiver

例如,这是我的代码:

private class MyReceiver extends BroadcastReceiver{
         @Override
         public void onReceive(Context arg0, Intent arg1) {
          datapassed = arg1.getStringExtra("Webresponse");
            try {
                XmlToDocument xmltodoc = new XmlToDocument();
                Document doc = xmltodoc.getDomElement(datapassed);
                // normalize text representation
                doc.getDocumentElement().normalize();
                NodeList listOfPersons = doc
                        .getElementsByTagName("user");

                adapter.clear();
                for (int s = 0; s < listOfPersons.getLength(); s++) {
                    StructPasokhgoo stp = new StructPasokhgoo();
                    Node firstPersonNode = listOfPersons.item(s);
                    if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {

                        Element firstPersonElement = (Element) firstPersonNode;
                        NodeList ageList = firstPersonElement
                                .getElementsByTagName("group");
                        Element ageElement = (Element) ageList
                                .item(0);

                        NodeList textAgeList = ageElement
                                .getChildNodes();
                        group = ((Node) textAgeList.item(0))
                                .getNodeValue().trim();
                        if (group.equals("admin")) {
                            //----//
                            NodeList firstNameList = firstPersonElement
                                    .getElementsByTagName("id");
                            Element firstNameElement = (Element) firstNameList
                                    .item(0);

                            NodeList textFNList = firstNameElement
                                    .getChildNodes();
                            id = ((Node) textFNList.item(0))
                                    .getNodeValue().trim();
                            //----//
                            NodeList lastNameList = firstPersonElement
                                    .getElementsByTagName("name");
                            Element lastNameElement = (Element) lastNameList
                                    .item(0);

                            NodeList textLNList = lastNameElement
                                    .getChildNodes();
                            name = ((Node) textLNList.item(0))
                                    .getNodeValue().trim();
                            stp.NameS = name;
                            stp.Group = group;
                            stp.IdS = id;
                            pasokhgo.add(stp);

                        }

                        if (group.equals("user")) {
                            //----//
                            NodeList firstNameList = firstPersonElement
                                    .getElementsByTagName("id");
                            Element firstNameElement = (Element) firstNameList
                                    .item(0);

                            NodeList textFNList = firstNameElement
                                    .getChildNodes();
                            idTwo = ((Node) textFNList.item(0))
                                    .getNodeValue().trim();
                            //----//
                            NodeList lastNameList = firstPersonElement
                                    .getElementsByTagName("name");
                            Element lastNameElement = (Element) lastNameList
                                    .item(0);

                            NodeList textLNList = lastNameElement
                                    .getChildNodes();
                            nameTwo = ((Node) textLNList.item(0))
                                    .getNodeValue().trim();
                            //----//
                            stp.NameE = nameTwo;
                            stp.Group = group;
                            stp.IdE = idTwo;
                            pasokhgo.add(stp);

                        }
                    }
                }
                adapter.notifyDataSetChanged();
            } catch (Throwable t) {
                t.printStackTrace();
            }
         }
        }
私有类MyReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文arg0,意图arg1){
datapassed=arg1.getStringExtra(“Webresponse”);
试一试{
XmlToDocument xmltodoc=新的XmlToDocument();
Document doc=xmltodoc.getdoElement(数据传递);
//规范化文本表示
doc.getDocumentElement().normalize();
节点列表listOfPersons=doc
.getElementsByTagName(“用户”);
适配器。清除();
对于(int s=0;s

我遇到了这个问题,我无法在活动中获取数据,我只能看到toast,但我在
MyReceiver.class

中填充
listView
。你的意思是你需要在你的活动中传递
datapassed
的值吗?