第一步.MainActivity.java
package com.chaowen;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button startButton,stopButton,bindButton,unbindButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = (Button)findViewById(R.id.startButton);
stopButton = (Button)findViewById(R.id.stopButton);
bindButton = (Button)findViewById(R.id.bindButton);
unbindButton = (Button)findViewById(R.id.unbindButton);
//添加监听器
startButton.setOnClickListener(startListener);
stopButton.setOnClickListener(stopListener);
bindButton.setOnClickListener(bindListener);
unbindButton.setOnClickListener(unbindListener);
}
//启动Service监听器
private OnClickListener startListener = new OnClickListener() {
@Override
public void onClick(View v) {
//创建Intent
Intent intent = new Intent();
//设置Action属性
intent.setAction("com.chaowen.action.MY_SERVICE");
//启动该Service
startService(intent);
}
};
//停止Service监听器
private OnClickListener stopListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.chaowen.action.MY_SERVICE");
stopService(intent);
}
};
//连接对象
private ServiceConnection connection =new ServiceConnection() {
//断开时调用
@Override
public void onServiceDisconnected(ComponentName name) {
//输出日志
Log.i("Service", "断开连接");
//通过Toast显示信息
Toast.makeText(MainActivity.this, "断开连接", Toast.LENGTH_LONG).show();
}
//连接时调用
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//输出日志
Log.i("Service", "连接成功");
//通过Toast显示信息
Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_LONG).show();
}
};
//绑定Service监听器
private OnClickListener bindListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.chaowen.action.MY_SERVICE");
bindService(intent, connection, Service.BIND_AUTO_CREATE);
}
};
//解除绑定Service监听器
private OnClickListener unbindListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.chaowen.action.MY_SERVICE");
unbindService(connection);
}
};
}
第二步.MyService.java
package com.chaowen;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
Log.i("SERVICE", "onBind");
Toast.makeText(MyService.this, "onBind...", Toast.LENGTH_LONG).show();
return null;
}
@Override
public void onCreate() {
Log.i("SERVICE", "onCreate");
Toast.makeText(MyService.this, "onCreate...", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
Log.i("SERVICE", "onStart");
Toast.makeText(MyService.this, "onStart...", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
Log.i("SERVICE", "onDestroy");
Toast.makeText(MyService.this, "onDestroy...", Toast.LENGTH_LONG).show();
}
}
第三步.main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:text="启动Service"
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="停止Service"
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="绑定Service"
android:id="@+id/bindButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="解除绑定"
android:id="@+id/unbindButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
第四步.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chaowen"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<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="com.chaowen.action.MY_SERVICE" />
</intent-filter>
</service>
</application>
</manifest>