標籤:
service 四大組件之一,開啟後可以一直運行在系統後台。至於其他概念性問題我就不說了,直接操作把 具體內容在代碼注釋中有
先看下
上布局檔案把
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 8 <TextView 9 android:id="@+id/textView1"10 android:layout_width="wrap_content"11 android:layout_height="wrap_content"12 android:text="@string/hello_world" />13 14 <Button15 android:id="@+id/button1"16 android:layout_width="wrap_content"17 android:layout_height="wrap_content"18 android:text="啟動服務"19 android:onClick="startService" />20 21 <Button22 android:id="@+id/button2"23 android:layout_width="wrap_content"24 android:layout_height="wrap_content"25 android:text="停止服務"26 android:onClick="stopService" />27 28 <Button29 android:id="@+id/button3"30 android:layout_width="wrap_content"31 android:layout_height="wrap_content"32 android:text="綁定服務"33 android:onClick="bindService" />34 35 <Button36 android:id="@+id/button4"37 android:layout_width="wrap_content"38 android:layout_height="wrap_content"39 android:text="解除綁定服務"40 android:onClick="unbindService" />41 42 </LinearLayout>
重點看java代碼把
package com.zw.service;import android.os.Bundle;import android.os.IBinder;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.view.Menu;import android.view.View;public class MainActivity extends Activity {private Intent intent;private MyServiceCon conn = new MyServiceCon();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);intent = new Intent("com.zw.service.MyService");//Intent intent = new Intent(this,MyService.class);}public void startService(View v){startService(intent);}public void stopService(View v){stopService(intent);}//綁定服務public void bindService(View v){bindService(intent, conn, Context.BIND_AUTO_CREATE);}public void unbindService(View v){unbindService(conn);}//class MyServiceCon implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//綁定完成後回調方法(Activity) 和 Service 的串連}@Overridepublic void onServiceDisconnected(ComponentName name) {//服務解除綁定就調}}}
service類
package com.zw.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService extends Service {@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println("MyService-----onCreate");}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);System.out.println("MyService-----onStart");}@Overridepublic IBinder onBind(Intent arg0) {System.out.println("MyService-----onBind");return null;}@Overridepublic boolean onUnbind(Intent intent) {System.out.println("MyService-----onUnbind");return super.onUnbind(intent);}@Overridepublic void onDestroy() {// TODO Auto-generated method stubSystem.out.println("MyService-----onDestroy");super.onDestroy();}}
接下來要說的是在資訊清單檔中配置service
<service android:name="com.zw.service.MyService"> <intent-filter > <action android:name="com.zw.service.MyService"/> </intent-filter> </service>
好了 自己運行看看把
android service基本操作