Android 从一个活动启动意向服务,并将结果发送到另一个活动
我有一个android应用程序,其中有一个活动(活动A),显示一个图像3秒钟(启动屏幕),然后主活动(活动B)将启动 在我的主要活动中,我启动了一个从web获取数据的服务,但这项工作需要一些时间,用户会感到不舒服 我想要的是从活动A启动服务,并将结果发送到活动B,以向用户显示一些结果。问题是我在活动A中没有活动B的实例,无法将Android 从一个活动启动意向服务,并将结果发送到另一个活动,android,android-intent,intentservice,nested-class,Android,Android Intent,Intentservice,Nested Class,我有一个android应用程序,其中有一个活动(活动A),显示一个图像3秒钟(启动屏幕),然后主活动(活动B)将启动 在我的主要活动中,我启动了一个从web获取数据的服务,但这项工作需要一些时间,用户会感到不舒服 我想要的是从活动A启动服务,并将结果发送到活动B,以向用户显示一些结果。问题是我在活动A中没有活动B的实例,无法将ResultReceiver实例发送到IntentService 我该怎么做 我在活动B中有一个嵌套类,它扩展了ResultReceiver,听起来您需要一个数据模型来保存
ResultReceiver
实例发送到IntentService
我该怎么做
我在活动B中有一个嵌套类,它扩展了
ResultReceiver
,听起来您需要一个数据模型来保存活动之间的数据
一种解决方案是创建一个静态类或使用singleton设计模式。这可能会从服务中获取结果。您将在活动A中对此进行初始化,并在触发活动B的意图之前启动服务
然后在活动B中,您可以发送一个调用a方法来注册它的回调函数。如果有数据,请直接返回回拨功能。如果没有,则在从函数调用新类时执行此操作
然后,您的数据将被共享,并且仅在需要时获取/刷新
//------------类来保存数据项-----------
//------------接口将其连接在一起--------
//------------用于数据获取的单例类-----------
}遵循此操作。如果活动B创建成功,但服务仍在运行,该怎么办?我想在活动B中使用类似ResultReceiver的东西,因此每当服务完成时,接收器就会自动启动。您需要实现一个具有保证功能的接口。传入活动B作为引用。当数据可用时,新类将调用函数。如果数据已经可用,您将得到一个实例回调,如果不可用,它将在可用时进行回调。如果你喜欢,我可以编一些代码给你看?是的,我很感激。代码补充。我没有测试它,但展示了在类之间拥有共享模型/数据处理程序的想法。您可以从活动A调用相同的DataFetcher.fetchData并忽略那里的结果。谢谢。我会调查的。让你知道
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);
}
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();
}
}
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);
}