[英]Start intent service from one activity and send result to another
我有一個帶有活動的Android應用程序( 活動A ),顯示3秒的圖像(啟動畫面),之后主活動( 活動B )將開始。
在我的主要活動中,我啟動了一項服務,從網絡上獲取一些數據,但這項工作需要一些時間,用戶會感到不舒服。
我想要的是從活動A啟動服務並將結果發送到活動B以向用戶顯示一些結果。 問題是我在活動A中沒有活動B的實例來將ResultReceiver
實例發送到IntentService
。
我怎樣才能做到這一點?
我在Activity B中有一個擴展ResultReceiver
的嵌套類。
聽起來你需要一個數據模型來保存活動之間的數據。
一種解決方案是創建靜態類或使用單例設計模式。 這可以從服務中獲得結果。 您可以在活動A中初始化它並在激活活動B的意圖之前啟動服務
然后在活動B中,您可以發送一個調用方法來注冊它的回調函數。 如果數據在那里直接返回到回叫功能。 如果在從函數中調用新類時不這樣做。
然后,您的數據將被共享,並且只在您需要時才會獲取/刷新。
// -----------用於保存數據項的類-----------
public class DataItem {
private Integer id = -1;
// My Data stuff
public Integer getId() {
return this.id;
}
}
// -----------將它們連接在一起的界面-----------
import java.util.ArrayList;
public interface NotifiyDataInterface {
public void completedDataLoad( ArrayList data, String status);
}
// -----------用於數據提取的Singleton類-----------
public class DataFetcher {
static DataFetcher _instance = null;
ArrayList<NotifiyDataInterface> _callBackList = null;
ArrayList<DataItem> _cachedData = null;
String _dataStatus = "";
public DataFetcher() {
_callBackList = new ArrayList<NotifiyDataInterface>();
}
public static DataFetcher getInstance() {
if (DataFetcher._instance == null) {
DataFetcher._instance = new DataFetcher();
}
return _instance;
}
public void fetchData(NotifiyDataInterface _callBack) {
if (_cachedData != null) {
_callBack.completedDataLoad(this._cachedData, this._dataStatus);
return;
}
// Add to the call back list
this._callBackList.add(_callBack);
// Code to call your service to get data
//TODO: Add code to call your service
}
public void dataLoadComplete(ArrayList<DataItem> _newItems, String _fetchStatus) {
// Called from the service on a completed data load
this._cachedData = _newItems;
this._dataStatus = _fetchStatus;
NotifiyDataInterface _item = null;
for (int i = 0; i < _callBackList.size(); i++) {
_item = _callBackList.get(i);
_item.completedDataLoad(this._cachedData, this._dataStatus);
}
// Clear out the call back list
_callBackList.clear();
}
}
// -----------活動B的類-----------
public class ActivityB extends ActionBarActivity implements NotifiyDataInterface {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
// Get the single instance from the singleton pattern
DataFetcher dataFetcher = DataFetcher.getInstance();
dataFetcher.fetchData(this);
}
// Here is where you call back is fired
public void completedDataLoad( ArrayList data, String status) {
//TODO: Your Code to call on data load here
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.layout.activity_b, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.