android中IntentService和BroadcastService的应用

对之前开发的一个android应用的总结

之前开发了一个android的小程序,如果不使用工作线程,那么界面主线程就会出现ANR(Android No Response)问题。在这里,使用了Android提供的IntentService创建一个线程完成任务。当任务完成,线程结束时,使用一个BroadcastReceiver来通知主线程工作线程任务完成。

1. 使用IntentService创建一个工作线程

1.1. 首先,创建一个继承自IntentService的java类

import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;


public class InfoCollectService extends IntentService {
	public InfoCollectService(){
		super("InfoCollectService");
		Log.v("zjm", "InfoCollectService");
	}
	@Override
	protected void onHandleIntent(Intent intent) {
		// TODO Auto-generated method stub
		Log.v("zjm","begin IntentService");
		// 线程需要完成的主要工作,添加到下面;  
	}
}

1.2. 在AndroidManifest.xml的application项中,增加service

        <service android:name=".InfoCollectService"></service>

1.3. 在Activity中,加入对这个IntentService子类的调用

      
        Log.v("zjm", "before startService");        
        Intent infoCollectService = new Intent(this, InfoCollectService.class);
        infoCollectService.putExtra("test", "helloworld");
        startService(infoCollectService);
        Log.v("zjm", "afer startService");

2. 使用BroadcastReceiver通知工作线程完成

2.1. 在Activity类中,增加BroadcastReceiver的子类,接收工作线程结束消息;

   public class MyBroadcastReceiver extends BroadcastReceiver{
   	@Override
   	public void onReceive(Context context, Intent intent) {
   		if (intent.getAction() == ACTION_TYPE_THREAD ){
   	        TextView tv = (TextView)findViewById(R.id.t1);
   	        tv.setText("费时操作完成,请继续其他操作");				
   		}
   	}
   } 

2.2. 在Activity的OnCreate中,注册这个BroadcastReceiver的接收类;

       mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
       MyBroadcastReceiver mbr = new MyBroadcastReceiver();
       IntentFilter intentFilter = new IntentFilter();
       intentFilter.addAction(ACTION_TYPE_THREAD);
       mLocalBroadcastManager.registerReceiver(mbr, intentFilter);

2.3. 工作线程结束后,对主线程发送广播通知;

   // 耗时工作完成后,向主线发送通知
   Intent it = new Intent(InfoCollectActivity.ACTION_TYPE_THREAD);
   LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
   lbm.sendBroadcast(it);
   Log.v("zjm","finish IntentService");        

3. 完整代码

3.1 Activity类

public class InfoCollectActivity extends Activity {
	private LocalBroadcastManager mLocalBroadcastManager;
	public final static String ACTION_TYPE_THREAD = "action.type.thread";
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_collect);
        
        Log.v("zjm", "register broadcast receiver");    
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
        MyBroadcastReceiver mbr = new MyBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_TYPE_THREAD);
        mLocalBroadcastManager.registerReceiver(mbr, intentFilter); 
               
        Log.v("zjm", "before startService");        
        Intent infoCollectService = new Intent(this, InfoCollectService.class);
        startService(infoCollectService);
    
        Log.v("zjm", "afer startService");
    }

	public class MyBroadcastReceiver extends BroadcastReceiver{
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction() == ACTION_TYPE_THREAD ){
		        TextView tv = (TextView)findViewById(R.id.t1);
		        tv.setText("读取完成,请在计算机上继续操作");				
			}
		}
	}
}

3.2. 工作线程类

import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;


public class InfoCollectService extends IntentService {
	public InfoCollectService(){
		super("InfoCollectService");
		Log.v("zjm", "InfoCollectService");
	}
	@Override
	protected void onHandleIntent(Intent intent) {
		// TODO Auto-generated method stub
		Log.v("zjm","begin IntentService");
		// 线程需要完成的主要工作,添加到下面;

		// 耗时工作完成后,向主线发送通知
	        Intent it = new Intent(InfoCollectActivity.ACTION_TYPE_THREAD);
    	    	LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
   	     	lbm.sendBroadcast(it);
        	Log.v("zjm","finish IntentService");        
	}
}

4. 总结

Android对线程,消息进行了大量的封装。android中的service,其实质就是一个线程。而BroadcastReceiver,其实质就是向界面线程发送一个消息。

猜你喜欢

转载自blog.csdn.net/harborian/article/details/84259646