我在一个单独的文件中有一个AsyncTask(因为它使用了我大约一半的活动)而且在AsyncTask中我有一个带有上下文的构造函数,所以我可以显示进度对话框等。只有我遇到的问题是上下文不仅包含StartActivityForResult
StartActivity
。任何想法如何从另一个活动完成活动,因为我无法发送SetResult()
?
这是我的AsyncClass代码:
public class AsyncClass extends AsyncTask<String, Integer, Boolean> {
private ProgressDialog progressDialog;
private Context context;
private String message;
private String url;
private String methodName;
private String get;
private List<Shops> list;
private LinearLayout linearLayout;
public AsyncClass(Context context, String message, String methodName,
String url, LinearLayout view) {
this.context = context;
this.message = message;
this.methodName = methodName;
this.url = url;
this.linearLayout = view;
initialize();
}
private void initialize() {
list = new ArrayList<ShopList>();
get = context
.getString(R.string.web_service_method_name_get);
}
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage(message);
progressDialog.show();
}
@Override
protected Boolean doInBackground(String... params) {
if (methodName.equalsIgnoreCase(get)) {
boolean isResultEmpty;
int totalPropertyCount;
SoapObject partialResult = SoapObjectOperations.InvokeMethod(url,
methodName);
SoapObject result = (SoapObject) partialResult.getProperty(0);
totalPropertyCount = result.getPropertyCount();
if (totalPropertyCount > 0) {
for (int detailCount = 0; detailCount < totalPropertyCount; detailCount++) {
SoapPrimitive soapPrimitive = (SoapPrimitive) result
.getProperty(detailCount);
String name = soapPrimitive.getAttribute("name").toString();
String id = soapPrimitive.toString();
Shop shop = new Shop(id, name);
list.add(shop);
}
}
if (list.isEmpty()) {
isResultEmpty = true;
} else {
isResultEmpty = false;
}
return isResultEmpty;
}
else {
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (methodName.equalsIgnoreCase(get)) {
if (result) {
TextView textViewEmpty = new TextView(context);
textViewEmpty
.setText("Bla Bla Bla");
linearLayout.addView(textViewEmpty);
} else {
for (int i = 0; i < list.size(); i++) {
Button button = new Button(context);
button.setText(list.get(i).getName());
button.setId(list.get(i).getId());
button.setOnClickListener(new OpenShop());
linearLayout.addView(button);
}
}
}
}
class OpenShop implements View.OnClickListener {
@Override
public void onClick(View view) {
ShopDetail.SetId(view.getId());
Intent intent = new Intent(view.getContext(), ShopDetail.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
}
答案 0 :(得分:0)
问题并不完全清楚,但是通过意图表明活动结束的方式。您可以在活动完成后让您的活动广播私人意图,原始应用程序将收到此意图,甚至可以从中获取结果。