天天看點

.Net程式員玩轉Android開發---(19)Android IntentService服務

           Intentservice服務也是安卓中的一個服務,它繼承與service,但與servcie有所不同,它新開啟一個線程來處理所有的請求, 不需要再UI等待處理,onStartCommand方法把所有請求發送到工作隊列中,,然後再由工作隊列發送到onHandlerIntent中進行處理

           1.interservice預設情況下是新開啟一個線程來處理任務, service預設是在ui線程執行

           2. interservice會建立一個工作隊列來處理接受到任務,隻用目前任務處理完成,才能處理下一個任務,否則下一個任務一直處于等待狀态.

           3.所有的請求任務處理完成後,系統會自動停止服務,不需要手動停止stopselft

             下面的例子展示intentservice不斷接受外部傳了的消息,并按順序處理,首先在onStartCommand裡面接受消息存入隊列,然後在onHandleIntent進行處理

.Net程式員玩轉Android開發---(19)Android IntentService服務

          1.建立intentservice服務

package com.example.helloword;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

public class ReceiveIntentService extends IntentService  {

	String receivemsg;
	
	private Handler handler = new Handler() {  
		
		       public void handleMessage(android.os.Message msg) {  
		           Toast.makeText(ReceiveIntentService.this, " 接受消息:" + receivemsg, Toast.LENGTH_LONG).show();  
		       }  
	    };  

	
	public ReceiveIntentService() {
		super("1111");
		// TODO Auto-generated constructor stub
	}

	
	public ReceiveIntentService(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}
	
	
	@Override  
	   public int onStartCommand(Intent intent, int flags, int startId) {  
		
		Bundle bundle = intent.getExtras();  
		receivemsg= bundle.getString("para");  
	     return super.onStartCommand(intent, flags, startId);  
	   }  

	
	@Override
	protected void onHandleIntent(Intent intent) {
		// TODO Auto-generated method stub
		  try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  handler.sendEmptyMessage(0);  
	}
	
	
	

}
           

          2.通過activity不斷向intentservice發送消息

                布局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/etpostmsg"
        android:layout_width="275dp"
        android:layout_height="wrap_content" >
        
    </EditText>
    
    <Button
        android:id="@+id/btnpostmsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="發送請求" />

</LinearLayout>
           

      背景IntentActivity

package com.example.helloword;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class IntentActivity extends Activity {

	
	
	private Button btnpost;//啟動服務
	
	private EditText etmsg;
	
	Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.intentlayout);
		btnpost=(Button)findViewById(R.id.btnpostmsg);
		etmsg=(EditText)findViewById(R.id.etpostmsg);

		btnpost.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				 intent = new Intent("com.example.helloword.ReceiveIntentService");  
				Bundle bundle = new Bundle();  
				 bundle.putString("para",etmsg.getText().toString());  
				  intent.putExtras(bundle);      
			     startService(intent);      
			}
			
			
		});
		
		

	}
}
           

   AndroidManifest.xml添加配置

<activity android:name="com.example.helloword.IntentActivity">
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
          <service   android:name=".ReceiveIntentService">  
   <intent-filter>  
        <action android:name="com.example.helloword.ReceiveIntentService" />  
        <category android:name="android.intent.category.default" /> 
   </intent-filter>  
</service> 
           



繼續閱讀