如何将ArrayList <string>从Activity传递给android?</string>中的服务

时间:2014-03-11 13:22:18

标签: android string service arraylist android-activity

我在SD卡中有一个文件并使用活动的AsyncTask读取它并将文件中存在的数据存储到ArrayList中,我想将ArrayList从MainActivity.java传递给MyService.java。请帮助我...

使用MainActivity.java中的AsyncTask从SD卡读取文件,如下所示

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;

    public class MainActivity extends Activity 
    {
        String name = "/sdcard/Download/example.txt";
        ArrayList<String> lines = new ArrayList<String>();
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            AsyncTaskRunner reader = new AsyncTaskRunner();
            reader.execute(name);
        }
        //Async Task
        private class AsyncTaskRunner extends AsyncTask<String, String, String> 
        {
            String res;
            @Override
            protected String doInBackground(String... params) {
                publishProgress("Reading...."); // Calls onProgressUpdate()
                try 
                {
                    // Do your long operations here and return the result
                    //readFileLines(params[0]);
                    readFile(params[0]);


                }  catch (Exception e) {
                    e.printStackTrace();
                    res = e.getMessage();
                }
                return res;
            }

            /*
             * (non-Javadoc)
             * 
             * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
             */
            @Override
            protected void onPostExecute(String result) {
                // execution of result of Long time consuming operation
                Log.i("no. of lines",String.valueOf(lines.size()));
            }

            /*
             * (non-Javadoc)
             * 
             * @see android.os.AsyncTask#onPreExecute()
             */
            @Override
            protected void onPreExecute() {
                // Things to be done before execution of long running operation. For
                // example showing ProgessDialog
            }

            /*
             * (non-Javadoc)
             * 
             * @see android.os.AsyncTask#onProgressUpdate(Progress[])
             */
            @Override
            protected void onProgressUpdate(String... text) {

                // Things to be done while execution of long running operation is in
                // progress. For example updating ProgessDialog
            }
        }
        private void readFile(String name) throws IOException
        {

            File file = new File(name);

            if(file.exists())   // check if file exist
            {
                //Read text from file
                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    Log.i("async", "reading started");
                    lines.clear();
                    String strLine;
                    while (true)
                    {
                        strLine = br.readLine();

                        if (strLine == null)
                        {
                            br.close();
                            Log.i("complete", String.valueOf(lines.size()));
                            return;
                        }
                        lines.add(strLine);
                    }
                }
                catch(Exception e)
                {
                    Toast.makeText(this, "can't read", Toast.LENGTH_LONG).show();

                }
            }
        }
    }

上面的ArrayList行应该传递给Service。

尝试使用Intent i = new Intent(MainActivity.this,MyService.class); i.putStringArrayListExtra("lines", lines); startService(i); 但这不起作用,并尝试使用Parcelable,但卡住了请帮助我...

1 个答案:

答案 0 :(得分:0)

我使用以下方法将ArrayList保存到SharedPreferences:

SharedPreferences sp_arrayList = getSharedPreferences("arrayList", 0);
SharedPreferences.Editor editor = sp_arrayList.edit();

int listSize = arrayList.size();
editor.putInt("arrayList_size", listSize);

for (int i = 0; i < listSize; i++) {

    String myString = arrayList.get(i);
    editor.putString("myString_" + i, myString);
}

editor.commit();

但可能有更好的解决方案。