我可以从类扩展AsyncTask sendBroadcast吗?

时间:2011-08-01 04:14:08

标签: android multithreading interface broadcastreceiver

我有一个扩展AsyncTask的类。

当做后台任务完成后,在后执行中我得到其他类的静态属性等于做后台的结果,我希望广播它,以便其他类中的接收器将是更新接口

这里是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);
    }   
}

之后我无法编写sendBroadcast方法,它是什么时候未定义?

1 个答案:

答案 0 :(得分:0)

如果我理解你正确遵循是解决方案。

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

        }

    }

       }

}