Hello,

This is from 
http://developer.android.com/reference/android/content/BroadcastReceiver.html
"A BroadcastReceiver object is only valid for the duration of the call
to onReceive(Context, Intent). Once your code returns from this
function, the system considers the object to be finished and no longer
active." - it means that you can not just say: "aa.notifyDataSetChanged
(); " your "onReceive(Context, Intent)"  executes in different thread
than your main activity. So you should use "Handler"(http://
developer.android.com/reference/android/os/Handler.html).
I`d probably this way:

public MainActivity extends Activity{
 ....
final static int UPDATE_STATIONS = 1;

final Handler mainActivityHandler = new Handler(){

 public handleMessage(Message msg){
  switch(msg.what){
                case UPDATE_STATIONS:
                String sname = msg.peekData().getString("StationName");
                String sloc = msg.peekData().getString("StationLoc");
                String ids = imsg.peekData().getString("Sid");
                String dist = imsg.peekData().getString("Distance");
                String stype = msg.peekData().getString("Types");
                Stations station = new Stations
(sname,sloc,ids,dist,stype);
                stations.add(station);
                aa.notifyDataSetChanged();
                break;
 }
}


public class StationReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent){

                Message mes = Mesage.obtain(mainActivityHandler,
UPDATE_STATIONS);
                Bundle data = mes.peekData();
                data.putString("StationName", intent.getExtras
().getString("StationName"));
                data.putString("StationLoc", intent.getExtras
().getString("StationLoc"));
                data.putString("Sid", intent.getExtras().getString
("Sid"));
                data.putString("Distance", intent.getExtras().getString
("Distance"));
                data.putString("Types", intent.getExtras().getString
("Types"));
               mainActivityHandler.sendMessge(mes);
               mes = null;
        }
    }

}

Or to be more effective:)

public MainActivity extends Activity{
 ....
final static int UPDATE_STATIONS = 1;

final Handler mainActivityHandler = new Handler(){

 public handleMessage(Message msg){
  switch(msg.what){
                case UPDATE_STATIONS:
                String sname = msg.peekData().getString("StationName");
                String sloc = msg.peekData().getString("StationLoc");
                String ids = imsg.peekData().getString("Sid");
                String dist = imsg.peekData().getString("Distance");
                String stype = msg.peekData().getString("Types");
                Stations station = new Stations
(sname,sloc,ids,dist,stype);
                stations.add(station);
                aa.notifyDataSetChanged();
                break;
 }
}


public class StationReceiver extends BroadcastReceiver{
        Weakreference<Handler> mHandler = new WeakReference<Handler>
(MainActivity.this.mainActivityHandler);
        @Override
        public void onReceive(Context context, Intent intent){
                Handler target = mHandler.get();
                if(target != null){
                Message mes = Mesage.obtain(target, UPDATE_STATIONS);
                Bundle data = mes.peekData();
                data.putString("StationName", intent.getExtras
().getString("StationName"));
                data.putString("StationLoc", intent.getExtras
().getString("StationLoc"));
                data.putString("Sid", intent.getExtras().getString
("Sid"));
                data.putString("Distance", intent.getExtras().getString
("Distance"));
                data.putString("Types", intent.getExtras().getString
("Types"));
               target.sendMessge(mes);
               mes = null;
              }
        }
    }

}
}


I hope this would help.

On 12 янв, 22:17, Tommy <[email protected]> wrote:
> I have created an activity that calls a service. This service receive
> an XML feed and parse the info. Each time I get the info for a new
> station i want to send the details (Name,Id,Location all as string)
> back to my main activity so it can load it inside a list view. When I
> run my code inside the main activity it works. When I move it to a
> service I cant send the data back. This is what I have tried.
>
> After getting the current station info from I do this:
> submitStation(namer, loc, ids, dis, types);
>
> then:
>
> private void submitStation(String names, String loca, String idss,
> String diss, String typess){
>
>         Intent intent = new Intent(NEW_STATION_LISTING);
>         intent.putExtra("StationName",names);
>         intent.putExtra("Sid", loca);
>         intent.putExtra("StationLoc", idss);
>         intent.putExtra("Distance", diss);
>         intent.putExtra("Types", typess);
>
>         sendBroadcast(intent);
>
> }
>
> Now in my main activity I have this:
>
>  public class StationReceiver extends BroadcastReceiver{
>         @Override
>         public void onReceive(Context context, Intent intent){
>
>                 String sname = intent.getExtras().getString("StationName");
>                 String sloc = intent.getExtras().getString("StationLoc");
>                 String ids = intent.getExtras().getString("Sid");
>                 String dist = intent.getExtras().getString("Distance");
>                 String stype = intent.getExtras().getString("Types");
>                 Stations station = new Stations(sname,sloc,ids,dist,stype);
>                 stations.add(station);
>                 aa.notifyDataSetChanged();
>         }
>     }
>
> Any help is greatly appreciated!
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to