I have a class that extends the AsyncTask.
When the doing background task done, in post executed i get the static properties of other class to equal the result of doing background, n I WANT TO BROADCAST IT OUT SO THAT the on开发者_如何学Python receiver in other class will be update interface
here the code of the onPostExecuted
protected void onPostExecute(String result) {
Log.d(tag, "post executed "+result);
// do sth here
if (result != null){
result = result.trim();
String temp_result[];
if ( result.contains("|") ){
temp_result = result.split("\\|");
MyGPS.location_info = temp_result[1];//
Log.d(result, "contains | : "+MyGPS.location_info);
}else if (result.equalsIgnoreCase("300 OK")){
Log.d(result, "in 300 OK BUT UNKNOWN : "+ result);
MyGPS.location_info = "Unknown";
}else if (result.equalsIgnoreCase("400 ERROR"))
Log.d(result, "400 ERROR : "+ result);
else Log.d(result, "else : "+ result);
//assemble data bundle to be broadcasted
//myFilteredResponseThread = new Intent(GPS_FILTER);
myFilteredResponseThread.putExtra("location_info_post",
MyGPS.location_info);
// CAN"T USE SEND BROADCAST METHOD ?
//myFilteredResponseThread.
//Log.e(">>GPS_Service<<", "location_info"+MyGPS.location_info);
}
}
after that i can't write sendBroadcast method , it is undefined whY?
If I understood u correctly following is the solution.
class MyService extends Service{
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
class GPSListener implements LocationListener{
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
// you can get context as follow
MyService.this.getBaseContext().sendBroadcast(new Intent("Hi"));
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}
}
精彩评论