Android中的intentservice

来源:互联网 发布:xboxone安卓软件 编辑:程序博客网 时间:2024/05/21 06:42
 

Android中的intentservice

分类: JAVA 手机相关 427人阅读 评论(0)收藏 举报
在Android的应用中,往往需要在执行主界面的操作时,如果要执行耗时的操作,那么应该是另外开线程的,或者是用async或者handler,今天发现其实也可以用android中的一个Intentservice去实现。下面例子讲解下。

1 例子中是一个文本框,当用户输入内容后,模拟slepp 10秒,这个时候要是不分离线程,操作的话,用户再点界面,就会死死地停在那里,甚至是出现提示,要强行CLOSE,代码如下:
Java代码 复制代码
  1. EditText input = (EditText) findViewById(R.id.txt_input);   
  2.  String strInputMsg = input.getText().toString();     
  3.  SystemClock.sleep(30000); // 30 seconds, pretend to do work   TextView result = (TextView) findViewById(R.id.txt_result); result.setText(strInputMsg + " " + DateFormat.format("MM/dd/yy h:mmaa", System.currentTimeMillis()));   
view plaincopy to clipboardprint?
  1. EditText input = (EditText) findViewById(R.id.txt_input);  
  2.  String strInputMsg = input.getText().toString();    
  3.  SystemClock.sleep(30000); // 30 seconds, pretend to do work   TextView result = (TextView) findViewById(R.id.txt_result); result.setText(strInputMsg + " " + DateFormat.format("MM/dd/yy h:mmaa", System.currentTimeMillis()));   

 
2 下面是使用IntentService
   首先,我们搞一个类SimpleIntentService,继承了IntentService
Java代码 复制代码
  1. public class SimpleIntentService extends IntentService {   
  2.     public static final String PARAM_IN_MSG = "imsg";   
  3.     public static final String PARAM_OUT_MSG = "omsg";   
  4.   
  5.     public SimpleIntentService() {   
  6.         super("SimpleIntentService");   
  7.     }   
  8.   
  9.     @Override  
  10.     protected void onHandleIntent(Intent intent) {   
  11.   
  12.         String msg = intent.getStringExtra(PARAM_IN_MSG);   
  13.         SystemClock.sleep(3000); // 30 seconds  
  14.         String resultTxt = msg + " "  
  15.             + DateFormat.format("MM/dd/yy h:mmaa", System.currentTimeMillis());   
  16.         Log.v("SimpleIntentService""Handling msg: " + resultTxt);   
  17.   
  18.         Intent broadcastIntent = new Intent();   
  19.         broadcastIntent.setAction(ResponseReceiver.ACTION_RESP);   
  20.         broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);   
  21.         broadcastIntent.putExtra(PARAM_OUT_MSG, resultTxt);   
  22.         sendBroadcast(broadcastIntent);   
  23.     }  
view plaincopy to clipboardprint?
  1. public class SimpleIntentService extends IntentService {  
  2.     public static final String PARAM_IN_MSG = "imsg";  
  3.     public static final String PARAM_OUT_MSG = "omsg";  
  4.   
  5.     public SimpleIntentService() {  
  6.         super("SimpleIntentService");  
  7.     }  
  8.   
  9.     @Override  
  10.     protected void onHandleIntent(Intent intent) {  
  11.   
  12.         String msg = intent.getStringExtra(PARAM_IN_MSG);  
  13.         SystemClock.sleep(3000); // 30 seconds  
  14.         String resultTxt = msg + " "  
  15.             + DateFormat.format("MM/dd/yy h:mmaa", System.currentTimeMillis());  
  16.         Log.v("SimpleIntentService""Handling msg: " + resultTxt);  
  17.   
  18.         Intent broadcastIntent = new Intent();  
  19.         broadcastIntent.setAction(ResponseReceiver.ACTION_RESP);  
  20.         broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);  
  21.         broadcastIntent.putExtra(PARAM_OUT_MSG, resultTxt);  
  22.         sendBroadcast(broadcastIntent);  
  23.     }  

   我们将跟主界面线程分离的操作都写在这里的ononHandleIntent中,这里首先通过
主线程传递的Intent中,获得用户文本框中输入的内容,放到变量msg中,然后
又建立一个Intent,把结果放到这个Intent中去,然后再sendBroadcast(broadcastIntent)广播出去,丢回给主线程。

3 在主线程中,这样启动:
 
Java代码 复制代码
  1. EditText input = (EditText) findViewById(R.id.txt_input);   
  2.        String strInputMsg = input.getText().toString();   
  3.   
  4.        Intent msgIntent = new Intent(this, SimpleIntentService.class);   
  5.        msgIntent.putExtra(SimpleIntentService.PARAM_IN_MSG, strInputMsg);   
  6.        startService(msgIntent);  
view plaincopy to clipboardprint?
  1. EditText input = (EditText) findViewById(R.id.txt_input);  
  2.        String strInputMsg = input.getText().toString();  
  3.   
  4.        Intent msgIntent = new Intent(this, SimpleIntentService.class);  
  5.        msgIntent.putExtra(SimpleIntentService.PARAM_IN_MSG, strInputMsg);  
  6.        startService(msgIntent);  

 
4 同时,在主线程中,也要有一个receive接收
 
Java代码 复制代码
  1. public class ResponseReceiver extends BroadcastReceiver {   
  2.         public static final String ACTION_RESP = "com.mamlambo.intent.action.MESSAGE_PROCESSED";   
  3.         @Override  
  4.         public void onReceive(Context context, Intent intent) {   
  5.               
  6.             // Update UI, new "message" processed by SimpleIntentService  
  7.            TextView result = (TextView) findViewById(R.id.txt_result);   
  8.            String text = intent.getStringExtra(SimpleIntentService.PARAM_OUT_MSG);   
  9.            result.setText(text);   
  10.         }   
  11.            
  12.     }   
  13.   
  14.    
view plaincopy to clipboardprint?
  1. public class ResponseReceiver extends BroadcastReceiver {  
  2.         public static final String ACTION_RESP = "com.mamlambo.intent.action.MESSAGE_PROCESSED";  
  3.         @Override  
  4.         public void onReceive(Context context, Intent intent) {  
  5.              
  6.             // Update UI, new "message" processed by SimpleIntentService  
  7.            TextView result = (TextView) findViewById(R.id.txt_result);  
  8.            String text = intent.getStringExtra(SimpleIntentService.PARAM_OUT_MSG);  
  9.            result.setText(text);  
  10.         }  
  11.           
  12.     }  
  13.   
  14.    

  当然,要注册这个broadcastReceiver,
Java代码 复制代码
  1. public void onCreate(Bundle savedInstanceState) {   
  2.        super.onCreate(savedInstanceState);   
  3.        setContentView(R.layout.main);   
  4.           
  5.        IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);   
  6.        filter.addCategory(Intent.CATEGORY_DEFAULT);   
  7.        receiver = new ResponseReceiver();   
  8.        registerReceiver(receiver, filter);   
  9.    }  
view plaincopy to clipboardprint?
  1. public void onCreate(Bundle savedInstanceState) {  
  2.        super.onCreate(savedInstanceState);  
  3.        setContentView(R.layout.main);  
  4.          
  5.        IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);  
  6.        filter.addCategory(Intent.CATEGORY_DEFAULT);  
  7.        receiver = new ResponseReceiver();  
  8.        registerReceiver(receiver, filter);  
  9.    }  

  可以看到,intent service还是比较清晰简单的,但至于性能方面,还是要继续学习,
迟点继续研究下这玩意哦