Search code examples
androidandroid-intentandroid-serviceandroid-broadcast

How I can insert data in a String from registerReceiver (Service) ? Android


How I can insert data in a String from registerReceiver, I have below codes .

In my Service I writed this code :

        Intent ir=new Intent(); 
        ir.setAction(MY_ACTION);
        ir.putExtra("Webresponse", Webresponse); 
        sendBroadcast(ir);

And in Activity I get data :

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();
         }
    }

Then in onCreate I can get data and see data with Toast but I need that insert data in a String value:

MyReceiver m = new  MyReceiver();
                            IntentFilter intentFilter = new IntentFilter();
                            intentFilter.addAction(ChatPage_Service.MY_ACTION);
                            registerReceiver(m, intentFilter);

I need to :

String values = registerReceiver(m, intentFilter);

Solution

  • If you are fill a ListView you should fill it in your private class MyReceiver extends BroadcastReceiver .

    For example this is my code :

    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();
                }
             }
            }
    

    I had this problem and I can't get data in activity same you I can see just toast but then i fill listView in MyReceiver.class.