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

來源:互聯網
上載者:User

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

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

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

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

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

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

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);     }  @Overrideprotected void onHandleIntent(Intent intent) {// TODO Auto-generated method stub  try {Thread.sleep(5000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}  handler.sendEmptyMessage(0);  }}

 

 

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

布局檔案

 

                

後台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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.intentlayout);btnpost=(Button)findViewById(R.id.btnpostmsg);etmsg=(EditText)findViewById(R.id.etpostmsg);btnpost.setOnClickListener(new OnClickListener(){@Overridepublic 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);      }});}}

 

 

 

                                                                                                                             


 

 

??

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.