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

來源:互聯網
上載者:User

標籤:service服務   android開發   遊響雲停工作室   

      在做.NET開發的時候,我們經常會寫windows service服務,在後台運行。在android系統中,同樣可以寫android服務. Service是安卓四大組件一個非常重要的組件,

四大組件包括Activity, Service ,BroadCastReceive,Content Provicer, 前幾節課中,我們一直講解activity,這節我們看下怎樣使用Service, 我們在一些後台操作的時候,會使用service,比如在後台不斷擷取當前地理位置資訊。 並且service服務還可以與activity進行通訊。

         Android中所有的服務都繼承自Service,並且重載三個重要的方法

         onCreate:服務在第一次調用的時候執行,第二次如果服務沒有終止,再次調用的時候不會執行該方法

         onStart:服務啟動後,在該方法裡面可以監聽外部傳入的一些資訊

         onDestroy:服務終止時調用。

        我們看下面的例子

               

        1.建立服務

                    首先建立serviceone服務,繼承Service,

                   

package com.example.helloword;import android.app.Service;import android.content.Intent;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.widget.Toast;public class serviceone extends Service {@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn null;}public serviceone(){}@Overridepublic void onCreate() {// TODO Auto-generated method stub Toast.makeText(this, "onCreate建立服務", Toast.LENGTH_SHORT).show();   Log.i("service", "onDestroy");      super.onCreate();}@Overridepublic void onDestroy() {// TODO Auto-generated method stubToast.makeText(this, "服務停止", Toast.LENGTH_SHORT);  super.onDestroy();}@Override@Deprecatedpublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubLog.v("onstart", "接受訊息");Toast.makeText(this, "onStart接受訊息", Toast.LENGTH_SHORT).show();if (intent != null){Bundle bundle = intent.getExtras();  String para = bundle.getString("para");  Toast.makeText(this, "接受參數"+para, Toast.LENGTH_SHORT).show();}super.onStart(intent, startId);}    }


            服務建立完成後,要在AndroidManifest.xml添加服務

              

 <service   android:name=".serviceone">     <intent-filter>          <action android:name="com.example.helloword.serviceone" />          <category android:name="android.intent.category.default" />    </intent-filter>  </service>  


         2.服務和Activity通訊                 上面的服務建立完成後,我們現在建立一個activity向服務發送資訊,服務在onstart進行接收處理,首先建立布局檔案
                  serviceactivity.xml
<?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" >    <Button        android:id="@+id/btnstartservice"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="啟動服務" />    <Button        android:id="@+id/btnstopservice"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="停止服務" /></LinearLayout>

                建立ServiceActivity,在啟動服務的時候發送para參數               
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;public class ServiceActivity extends Activity {private Button btnstart;//啟動服務private Button btnstop;//停止服務Intent intent;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.serviceactivity);btnstart=(Button)findViewById(R.id.btnstartservice);btnstop=(Button)findViewById(R.id.btnstopservice);btnstart.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub intent = new Intent("com.example.helloword.serviceone");  Bundle bundle = new Bundle();   bundle.putString("para","Hello World");    intent.putExtras(bundle);           startService(intent);      }});btnstop.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubstopService(intent);}});}}



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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.