主要就是asynatask与handler的交互,希望大家可以参考,并指出问题.
service中的内容
public class MainActivity extends AppCompatActivity { public MainActivity() { } // private MyHandler myHandler; private Button bt; private ListView listView; public static MyHandler myHandler; public ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button) findViewById(R.id.start_servive); listView = (ListView) findViewById(R.id.listview); progressBar = (ProgressBar) findViewById(R.id.progressBar); myHandler = new MyHandler(); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,MyTestService.class); startService(intent); } }); } class MyHandler extends Handler{ public void handleMessage(Message msg) { // TODO Auto-generated method stub Log.d("MyHandler","handleMessage。。。。。。"); super.handleMessage(msg); // 此处可以更新UI switch (msg.what){ case 12: List<String> o = (List<String>) msg.obj; ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,o); listView.setAdapter(adapter); adapter.notifyDataSetChanged(); break; } super.handleMessage(msg); } }; public class MyTask extends AsyncTask<List, Integer,List> { //onPreExecute方法用于在执行后台任务前做一些UI操作 // private ProgressBar progressBar; // public MyTask(ProgressBar progressBar){ // this.progressBar = progressBar; // } //doInBackground方法内部执行后台任务,不可在此方法内修改UI private int percent = 0; @Override protected List doInBackground(List... param) { List<String> datas = new ArrayList<String>(); String path = Environment.getExternalStorageDirectory().getPath(); System.out.println("********************"+path); String content = ""; //文件内容字符串 File file = new File(path + "//" + "test.txt"); //如果path是传递过来的参数,可以做一个非目录的判断 if (file.isDirectory()) { Log.d("TestFile", "The File doesn't not exist."); } else { try { InputStream instream = new FileInputStream(file); if (instream != null) { InputStreamReader inputreader = new InputStreamReader(instream); BufferedReader buffreader = new BufferedReader(inputreader); String line; //分行读取 while ((line = buffreader.readLine()) != null) { percent+=1; System.out.println(percent); datas.add(line); publishProgress(percent); } instream.close(); } } catch (java.io.FileNotFoundException e) { Log.d("TestFile", "The File doesn't not exist."); } catch (IOException e) { Log.d("TestFile", e.getMessage()); } } Message message = new Message(); message.what = 12; message.obj = datas; MainActivity.myHandler.sendMessage(message); return datas; } //onProgressUpdate方法用于更新进度信息 @Override protected void onProgressUpdate(Integer... progresses) { int vlaue = progresses[0]; if(progressBar!=null){ progressBar.setProgress(vlaue); System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS"); } super.onProgressUpdate(progresses); } //onPostExecute方法用于在执行完后台任务后更新UI,显示结果 // @Override protected void onPostExecute(String result) { // progressBar.setVisibility(View.INVISIBLE); } public MyTask() { super(); } } }这个就是activity中的写的内容
public class MyTestService extends Service { private Handler myhandler; private ProgressBar progressBar; public MyTestService() { super(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("Service", "Service onStartCommand"); MainActivity mainActivity = new MainActivity(); MainActivity.MyTask myTask = mainActivity.new MyTask(); myTask.execute(); return super.onStartCommand(intent, flags, startId); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.d("jay","oncreate"); } }
service中的内容