2016-02-19 102 views
0

如何在另一個內部執行一個活動,而無需執行? 我的意思是,我有我的MainActivity其中我從ReadFromAssetActivity意圖獲得一些int數組。但要獲得這些int數組,我需要首先執行ReadFromAssetActivity,然後轉至MainActivity並執行它。 我想要的是按下MainActivity中的一個按鈕,該按鈕執行ReadFromAssetActivity,無需轉到該活動。如何使用另一個按鈕執行一個活動?

MainActivity

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Getting reference to the button btn_chart 
     Button btnChart = (Button) findViewById(R.id.btn_chart); 

     // Defining click event listener for the button btn_chart 
     View.OnClickListener clickListener = new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // Draw the Chart 
       openChart(); 
      } 
     }; 

     // Setting event click listener for the button btn_chart of the SensorGraph layout 
     btnChart.setOnClickListener(clickListener); 

    } 

    private void openChart() { 

     Intent intent = getIntent(); 

     int[] force_l = intent.getIntArrayExtra("force_l"); 
     int[] force_r = intent.getIntArrayExtra("force_r"); 
     int[] x = new int[force_l.length]; 
(...) 

ReadFromAssetActivity

public class ReadFromAssetActivity extends AppCompatActivity { 
    private Button read_Button; 
    int[] force_l = null; 
    int[] force_r = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_read_from_asset); 

     initializeUI(); 
    } 

    private void initializeUI() { 
     read_Button = (Button) findViewById(R.id.ReadFromAssetActivity_start_reading_button); 
     read_Button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       new ReadFromAsset().execute(); 
      } 
     }); 
    } 

    private class ReadFromAsset extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected Void doInBackground(Void... params) { 
      try { 

       BufferedReader reader = new BufferedReader(
         new InputStreamReader(getAssets().open("test.txt"))); 
       String mLine = null; 
       int count = 0; 
       while ((mLine = reader.readLine()) != null) { 
        String[] integer_Strings = mLine.split(" "); 
        //System.out.println(Arrays.deepToString(integer_Strings)); 
        if (count == 0) { 
         force_l = new int[integer_Strings.length]; 
         for (int i = 0; i < integer_Strings.length; i++) { 
          force_l[i] = Integer.parseInt(integer_Strings[i]); 
         } 
         count++; 
        } else if (count == 1) { 
         force_r = new int[integer_Strings.length]; 
         for (int i = 0; i < integer_Strings.length; i++) { 
          force_r[i] = Integer.parseInt(integer_Strings[i]); 
         } 
        } 

       } 

       Intent intent = new Intent(ReadFromAssetActivity.this, MainActivity.class); 
       intent.putExtra("force_l", force_l); 
       intent.putExtra("force_r", force_r); 
       startActivity(intent); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) { 
      super.onPostExecute(aVoid); 
     } 
    } 
} 
+0

那麼你爲什麼不把你所需的代碼放在'MainActivity'上? – starkshang

回答

2

你的意圖的實現幾乎沒有什麼純粹的Android的方式來做事。您不需要單獨的'ReadFromAssetActivity'或任何其他活動來運行您的AsyncTask。最好的辦法是將您的ReadFromAsset放入MainActivity中,並從openChart方法中調用。在回調界面中從AsyncTask獲取int數組。

如果您的任務實際上比您在此代碼段中向我們展示的方式複雜得多,請考慮爲此使用IntentService的可能性。

1

讓你ReadFromAsset任務一個單獨的類(不是內部類的活動),並把它存儲陣列作爲其「結果」。還要給任務一種記住「偵聽器」的方法,以便.onPostExecute()可以將這些數組發送回偵聽器(在本例中爲您的主要活動)。 我會列舉一個通用示例,但我在移動設備上,並且已經有很多示例。編輯:一個例子:

/* 
* This consumer could be anything -- an activity, a service, a data 
* object, whatever. All it needs to do is implement the task's 
* listener interface. 
*/ 
public class Example 
implements MyTask.Listener 
{ 
    // other code 

    public void doThings() 
    { 
     (new MyTask()).setListener(this) 
      .setInputFile("flargle.txt") 
      .execute() 
      ; 
    } 

    /** Specified by MyTask.Listener, called by MyTask#onPostExecute() */ 
    public void onMyTaskComplete(MyTaskResult res) 
    { 
     // Do something with the result. 
    } 
} 

public class MyTaskResult 
{ 
    // Stores whatever complex data you want the task to create. 
    // Necessary only if what you're creating is really complex. 
} 

public class MyTask extends AsyncTask<Void,Void,MyTaskResult> 
{ 
    public interface Listener 
    { 
     void onMyTaskComplete(MyTaskResult res) ; 
    } 

    protected Listener m_lstn ; 
    protected String m_sFilename ; 

    public MyTask setListener(Listener lstn) 
    { 
     m_lstn = lstn ; 
     return this ; 
    } 

    public MyTask setInputFile(String sFilename) 
    { 
     m_sFilename = sFilename ; 
     return this ; 
    } 

    @Override 
    public MyTaskResult doInBackground(Void... aNulls) 
    { 
     MyTaskResult res = new MyTaskResult() ; 
     // Do stuff that produces a MyTaskResult. 
     return res ; 
    } 

    @Override 
    public void onPostExecute(MyTaskResult res) 
    { 
     m_lstn.onMyTaskCompleted(res) ; 
    } 
} 
0

只要能使你的ReadFromAssetActivity一個公共靜態方法。現在你可以在那裏添加你需要的東西。您可以根據您的要求調用該方法並從主要活動執行。

相關問題