Android Services使用樣本

來源:互聯網
上載者:User

轉自:http://www.javaeye.com/topic/568547

Android開發中,當需要建立在後台啟動並執行程式的時候,就要使用到Service。Service 可以分為有無限生命和有限生命兩種。

特別需要注意的是Service跟Activities是不同的(簡單來說可以理解為後台與前台的區別),例如,如果需要使用Service的話,需要調用startService(),從而利用 startService()去調用Service中的OnCreate()和onStart()方法來啟動一個背景Service。

 

啟動一個Service的過程如下:

context.startService()  ->onCreate()- >onStart()->Service running

其中onCreate()可以進行一些服務的初始化工作,onStart()則啟動服務。

 

停止一個Service的過程如下:

context.stopService() | ->onDestroy() ->Service stop

 

 

接下來的執行個體是一個利用後台服務播放音樂的小例子,點擊start運行服務,點擊stop停止服務。

 

1.ServiceDemo.java

public class ServiceDemo extends Activity {<br /> /** Called when the activity is first created. */<br /> private Button button_start;<br /> private Button button_stop;<br /> private static final String TAG = "ServiceDemo";<br />@Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> button_start = (Button)findViewById(R.id.button_start);<br /> button_stop = (Button)findViewById(R.id.button_stop);<br /> button_start.setOnClickListener(new OnClickListener(){</p><p>@Override<br />public void onClick(View arg0) {<br />// TODO Auto-generated method stub<br />Log.d(TAG, "onClick: starting srvice");<br />//Intent intent = new Intent();<br />//intent.setClass(ServiceDemo.this, MyService.class);<br />startService(new Intent(ServiceDemo.this, MyService.class));<br />}});<br /> button_stop.setOnClickListener(new OnClickListener(){</p><p>@Override<br />public void onClick(View v) {<br />// TODO Auto-generated method stub<br />Log.d(TAG, "onClick: stop srvice");<br />stopService(new Intent(ServiceDemo.this, MyService.class));<br />}});<br /> }<br />}

2.MyService.java

public class MyService extends Service{</p><p>private static final String TAG = "MyService";<br />MediaPlayer player;<br />@Override<br />public IBinder onBind(Intent arg0) {<br />// TODO Auto-generated method stub<br />return null;<br />}</p><p>@Override<br />public void onCreate() {<br />// TODO Auto-generated method stub<br />Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();<br />Log.d(TAG, "onCreate"); </p><p>player = MediaPlayer.create(this, R.raw.take_me_to_your_heart);//運行例子是,音樂的名稱<br />player.setLooping(false); // Set looping<br />}</p><p>@Override<br />public void onDestroy() {<br />// TODO Auto-generated method stub<br />Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();<br />Log.d(TAG, "onDestroy");<br />player.stop();<br />}</p><p>@Override<br />public void onStart(Intent intent, int startId) {<br />// TODO Auto-generated method stub<br />Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();<br />Log.d(TAG, "onStart");<br />player.start();<br />}</p><p>}

3.main.xml

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><TextView<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="@string/hello"<br /> /><br /> <Button<br /> android:id="@+id/button_start"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="@string/str_start"<br /> /><br /> <Button<br /> android:id="@+id/button_stop"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="@string/str_stop"<br /> /><br /></LinearLayout>

4.AndroidManifest.xml

需要聲明service服務:

<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="com.richard"<br /> android:versionCode="1"<br /> android:versionName="1.0"><br /> <application android:icon="@drawable/icon" android:label="@string/app_name"><br /> <activity android:name=".ServiceDemo"<br /> android:label="@string/app_name"><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </activity><br /> <!--註冊service--><br /><service android:enabled="true" android:name=".MyService" /><br /> </application><br /> <uses-sdk android:minSdkVersion="3" /></p><p></manifest>

 

 

相關文章

聯繫我們

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