0

我有一個AsyncTask在單獨的文件(因爲它使用它關於我的一半活動),並在該AsyncTask我有一個構造函數的上下文,所以我可以顯示進度對話框等只有問題是上下文不包含StartActivityForResult只有StartActivity。任何想法如何從另一個活動完成活動,因爲我不能發送SetResult()Finish()來自其他人的活動,而不用StartActivityForResult()啓動它?

這裏是我的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

爲了榮譽我對Android很有新意,我不想告訴一些事件完成了,我想指出這一點:例子=我有2個活動,A和B,A開始B,用戶玩B然後點擊按鈕,B通常會執行setResult()並完成,活動A將重新啓動(對於已從數據庫中的活動B更改的更新數據) – Alone89

相關問題