android asynTask 到活動問題? (android asynTask to activity problem ?)


問題描述

android asynTask 到活動問題? (android asynTask to activity problem ?)

I develop an GPS app. my app have a service that responsible to send GPS to server through server....      

Service ‑‑‑‑‑> AysncTask(sendServer)
// then in service i broadcast some variable

myFilteredResponse = new Intent(GPS_FILTER);
        myFilteredResponse.putExtra("latitude",      Double.valueOf(twoFormat.format(location.getLatitude())));
        myFilteredResponse.putExtra("longitude", Double.valueOf(twoFormat.format(location.getLongitude())));
        myFilteredResponse.putExtra("network", MyGPS.cur_network_status);

then in my Activity I create a broadcast receiver to receive all these variable n it works fine, my UI then will change when we have new lat n lon

in my AysncTask when send to server i will get the location identify by server so i want to update my UI interface so i 1 2 use broadcast as well but it's not work !

structure 

                                  broadcast
service ( on location changed ) ‑‑‑‑‑‑‑‑‑‑‑‑‑>   UI interface

            broadcast??
AsyncTask ‑‑‑‑‑‑‑‑‑‑‑‑‑>   UI interface

i can't use sendBroadcast in my AsyncTask since my AsyncTask is not activity any Idea?

‑‑‑‑‑

參考解法

方法 1:

i guess you problem is to get the data from the service.

you can bind the service to the UI and call the service from within the activity.

here is an opensource project. Link.

this is the service code. Link.

this is the UI. Link

EDIT: if you want to do UI updates using AsyncTask then you can use onPostExecute which runs in UI thread.

EDIT: Here you go. AsyncTask Example. Link.

(by sayvortanaSamuel)

參考文件

  1. android asynTask to activity problem ? (CC BY‑SA 3.0/4.0)

#service #Android #android-asynctask






相關問題

服務間數據的參照完整性 (Referential Integrity of Data Between Services)

系統重新啟動時自動啟動星號 (start asterisk autometically when system got restarted)

CanStop 設置為 False 時停止 Windows 服務的方法 (C#) (Way to Stop a Windows Service when CanStop is set to False (C#))

Android: App Widget trong quá trình cập nhật hiển thị hình ảnh lạ (Android: App Widget during update shows strange images)

查找具有特定 startname 的特定服務 (Finding specific services with specific startname)

如何判斷方法是否從 .Net(託管)代碼中的 Windows 服務調用 (How to tell if method is called from Windows Service in .Net (managed) code)

如何使用主屏幕小部件按鈕關閉服務? (How to use a homescreen widget button to shutdown a service?)

如何將版本號放入 rdlc 文件中? (How do you put a version number into an rdlc file?)

android asynTask 到活動問題? (android asynTask to activity problem ?)

GetCurrentDirectory 並沒有真正返回可執行文件的路徑 (GetCurrentDirectory does not really return the path of the executable file)

通知未顯示,因為服務或警報管理器已被終止 (Notification not showing because service or alarm manager is killed off)

當 gunicorn / celery 服務重新啟動時,Django 中有沒有辦法只執行一次 python 代碼? (Is there a way in Django to execute some python code only once when gunicorn / celery services are getting restarted?)







留言討論