安卓开发service
如果把Activity比喻为前台程序,那么service可以看做是一个后台程序。Service跟Activity一样也由Intent调用。
在工程里想要添加一个Service,先新建继承Service的类,然后到AndroidManifest.xml -> Application
中的Service标签中添加。
如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.service" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyService"> <intent-filter> <action android:name="" /> </intent-filter> </service> </application> </manifest>
其中,第一个name是service的位置,包括完整的包名和service名,如果包名就是你定义的程序包名,也就是和gen目录下那个包的名字一样的话,直接".service名"就可以了。第二个name是你调用service时intent.setAction();中的参数,这个可以自己随便定义。
代码如下:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnStartMyService" android:text="StartMyService"> </Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnStopMyService" android:text="StopMyService"> </Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnBindMyService" android:text="BindMyService"> </Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnUnbindMyService" android:text="UnbindMyService"> </Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnExit" android:text="退出程序"> </Button> </LinearLayout>
MyService.java:
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 { static public String ServiceState=""; @Override public IBinder onBind(Intent arg0) { Log.e("Service", "onBind"); ServiceState="onBind"; return null; } @Override public boolean onUnbind(Intent intent){ super.onUnbind(intent); Log.e("Service", "onUnbind"); ServiceState="onUnbind"; return false; } @Override public void onCreate(){ super.onCreate(); Log.e("Service", "onCreate"); ServiceState="onCreate"; } @Override public void onDestroy(){ super.onDestroy(); Log.e("Service", "onDestroy"); ServiceState="onDestroy"; } @Override public void onStart(Intent intent,int startid){ super.onStart(intent, startid); Log.e("Service", "onStart"); ServiceState="onStart"; } }
MainActivity.java:
package com.example.service; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { Button btnStartMyService,btnStopMyService,btnBindMyService,btnUnbindMyService,btnExit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStartMyService=(Button)this.findViewById(R.id.btnStartMyService); btnStartMyService.setOnClickListener(new ClickEvent()); btnStopMyService=(Button)this.findViewById(R.id.btnStopMyService); btnStopMyService.setOnClickListener(new ClickEvent()); btnBindMyService=(Button)this.findViewById(R.id.btnBindMyService); btnBindMyService.setOnClickListener(new ClickEvent()); btnUnbindMyService=(Button)this.findViewById(R.id.btnUnbindMyService); btnUnbindMyService.setOnClickListener(new ClickEvent()); btnExit=(Button)this.findViewById(R.id.btnExit); btnExit.setOnClickListener(new ClickEvent()); } @Override public void onDestroy() { super.onDestroy(); Log.e("Activity","onDestroy"); } private ServiceConnection _connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName arg0, IBinder arg1) { // TODO Auto-generated method stub } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } }; class ClickEvent implements View.OnClickListener{ @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,MyService.class); if(v==btnStartMyService){ MainActivity.this.startService(intent); } else if(v==btnStopMyService){ MainActivity.this.stopService(intent); } else if(v==btnBindMyService){ MainActivity.this.bindService(intent, _connection, Service.BIND_AUTO_CREATE); } else if(v==btnUnbindMyService){ if(MyService.ServiceState=="onBind")//Service绑定了之后才能解绑 MainActivity.this.unbindService(_connection); } else if(v==btnExit) { MainActivity.this.finish(); } } } }