Android Service組件(1)

來源:互聯網
上載者:User

Android Service組件(1)
android service 和其他服務一樣,並沒有實際啟動並執行介面,它運行在android 後台。一般通過service為應用程式提供服務(比如,從Internet下載檔案,控制音樂播放器等)。Service的生命週期要比activity簡單的多,它只有三個階段(建立服務、開始服務、銷毀服務)。下面通過具體案例講解android的service組件。 1.建立android工程 在Eclipse中建立android工程 android_service_one 2.建立Service 在android工程中,建立包com.example.service,並添加方法MyService。該方法繼承與Service。該類用來展示服務的三個生命週期。    複製代碼package com.example.service; import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log; /* *  */public class MyService extends Service {     @Override    public IBinder onBind(Intent intent) {        // TODO Auto-generated method stub        return null;    }    //第一次開啟時調用    public void onCreate()    {        Log.d("MyService", "onCreate");        super.onCreate();    }    //停止時調用    public void onDestory()    {        Log.d("MySerVice", "onDestory");        super.onDestroy();    }    //開始時調用    public void onStart(Intent intent,int startId)    {        Log.d("MyService", "onStart");        super.onStart(intent, startId);    }}複製代碼3.調用服務 在android 活動組件中調用剛剛建立的服務,調用之前需要在AndroidManifest.xml中對該服務進行配置。通過<service></service>來佈建服務組件。 1   <service android:enabled="true" android:name="com.example.service.MyService"></service>服務配置完成以後,可以調用該服務。服務與activity之間的互動依然通過Intent來進行通訊。啟動服務通過startservice(intent),停止服務通過stopservice(Intent intent)來完成。 Intent的聲明:Intent Intent=new Intent(this,MyService.class);  複製代碼 1     public void onClick(View v) { 2         // TODO Auto-generated method stub 3         switch(v.getId()) 4         { 5         case R.id.button1: 6             startService(serviceintent); 7             break; 8         case R.id.button2: 9             stopService(serviceintent);10             break;11         }12     }複製代碼  4.Service與Activity綁定 Service與Activity綁定,當Activity啟動時,服務自動啟動。當Activity被銷毀時,服務也被銷毀。這裡需要在MyService中重寫 onBind、onRebind和onUnbind等幾個方法。  複製代碼 1 package com.example.service; 2  3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7 import android.util.Log; 8  9 /*10  * 11  */12 public class MyService extends Service {13 14     private MyBinder myBinder= new MyBinder();15     16     @Override17     //成功綁定時調用該方法18     public IBinder onBind(Intent intent) {19         // TODO Auto-generated method stub20         Log.d("MyService", "onBind");21         return myBinder;22     }23     24     //重新綁定時調用該方法25     public void onRebind(Intent intent)26     {27         Log.d("MyService", "onRebind");28         super.onRebind(intent);29     }30     31     //解除綁定時調用該方法32     public void onUnBind(Intent intent)33     {34         Log.d("MyService", "onUnbind");35         super.onUnbind(intent);36     }37     //第一次開啟時調用38     public void onCreate()39     {40         Log.d("MyService", "onCreate");41         super.onCreate();42     }43     //停止時調用44     public void onDestory()45     {46         Log.d("MySerVice", "onDestory");47         super.onDestroy();48     }49     //開始時調用50     public void onStart(Intent intent,int startId)51     {52         Log.d("MyService", "onStart");53         super.onStart(intent, startId);54     }55     56     //綁定類57     public class MyBinder extends Binder58     {59         //返回服務執行個體60         public MyService getService()61         {62             return MyService.this;63         }64     }65 }複製代碼5.調用服務 在Activity中聲明服務變數 Myservice myService,和聲明ServiceConnection串連變數。綁定變數調用bindService(serviceintent, conn, Context.BIND_AUTO_CREATE); 解除綁定調用,unbindService(ServiceConnection).  複製代碼 1     Intent serviceintent; 2     private MyService myService; 3     private ServiceConnection conn=new ServiceConnection() { 4          5         @Override 6         public void onServiceDisconnected(ComponentName name) { 7             // TODO Auto-generated method stub 8             myService=null; 9             Toast.makeText(MainActivity.this, "服務串連失敗", Toast.LENGTH_LONG).show();10         }11         12         @Override13         public void onServiceConnected(ComponentName name, IBinder service) {14             // TODO Auto-generated method stub15             myService=((MyService.MyBinder)service).getService();16             Toast.makeText(MainActivity.this, "服務串連成功", Toast.LENGTH_LONG).show();17         }18     };複製代碼

聯繫我們

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