1.创建界面布局activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button android:id="@+id/button_start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动Service"/>
<Button android:id="@+id/button_stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="停止Service"
android:layout_below="@id/button_start"/>
<Button android:id="@+id/button_bind"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="绑定Service"
android:layout_below="@id/button_stop"/>
<Button android:id="@+id/button_unbind"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="解除绑定"
android:layout_below="@id/button_bind"/>
</RelativeLayout>
2.创建Service服务MyService
public class MyService extends Service {
@Override
public IBinder onBind(Intent arg0) {
Log.i("MyService","onBind....ing......");
Toast.makeText(MyService.this, "onBind....ing......", Toast.LENGTH_LONG).show();
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i("MyService","onCreate....ing......");
Toast.makeText(MyService.this, "onCreate....ing......", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("MyService","onDestroy....ing......");
Toast.makeText(MyService.this, "onDestroy....ing......", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
Log.i("MyService","onStart....ing......");
Toast.makeText(MyService.this, "onStart....ing......", Toast.LENGTH_LONG).show();
}
}
3.创建Activity
public class MainActivity extends Activity {
protected ServiceConnection conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton=(Button)findViewById(R.id.button_start);
Button stopButton=(Button)findViewById(R.id.button_stop);
Button bindButton=(Button)findViewById(R.id.button_bind);
Button unbindButton=(Button)findViewById(R.id.button_unbind);
//连接对象
conn=new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("MyService", "连接成功!");
Toast.makeText(MainActivity.this, "连接成功!", Toast.LENGTH_LONG).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("MyService", "断开连接!");
Toast.makeText(MainActivity.this, "断开连接!", Toast.LENGTH_LONG).show();
}
};
startButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent();
//intent.setClass(MainActivity.this, MyService.class);
intent.setAction("com.dh.service.MY_SERVICE");
startService(intent);
}
});
stopButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setAction("com.dh.service.MY_SERVICE");
stopService(intent);
}
});
bindButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setAction("com.dh.service.MY_SERVICE");
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
unbindButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setAction("com.dh.service.MY_SERVICE");
unbindService(conn);
}
});
}
}
4.配置AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dh.service"
android:versionCode="1"
android:versionName="1.0" >
<permission android:protectionLevel="normal" android:name="com.dh.service.MY_SERVICE"></permission>
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.dh.service.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:permission="com.dh.service.MY_SERVICE"
android:name="com.dh.service.MyService"
android:enabled="true" >
<intent-filter >
<action android:name="com.dh.service.MY_SERVICE"/>
</intent-filter>
</service>
</application>
</manifest>