1. MyService
public class MyService extends Service {
private final static String TAG = "main";
public final static String SEND_OK_MESSAGE = "send.ok.message";
public final static String SEND_CANCLE_MESSAGE = "send.cancle.message";
private Context mContext;
private int count;
private MyBinder binder=new MyBinder();
private Thread thread;
private boolean quit;
@Override
public IBinder onBind(Intent arg0) {
if(mContext!=null){
Toast.makeText(mContext, "绑定成功", Toast.LENGTH_LONG).show();
}
return binder;
}
public boolean onUnbind(Intent intent) {
if(mContext!=null){
Toast.makeText(mContext, "解除绑定", Toast.LENGTH_LONG).show();
}
return true;
}
public void onCreate(){
super.onCreate();
thread=new Thread(new Runnable() {
@Override
public void run() {
while(!quit){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
count++;
}
}
});
thread.start();
}
//启动服务时,注册一个广播
public void onStart(Intent intent, int startId) {
IntentFilter myFilter = new IntentFilter();
myFilter.addAction(SEND_OK_MESSAGE);
myFilter.addAction(SEND_CANCLE_MESSAGE);
this.registerReceiver(myBroadCast, myFilter);
super.onStart(intent, startId);
}
private BroadcastReceiver myBroadCast = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//广播接收到消息
mContext = context;
String action = intent.getAction();
if (action.equals(SEND_OK_MESSAGE)) {
Toast.makeText(context, "接收到了一条广播为" + SEND_OK_MESSAGE, Toast.LENGTH_LONG).show();
}else if(action.equals(SEND_CANCLE_MESSAGE)) {
Toast.makeText(context, "接收到了一条广播为" + SEND_CANCLE_MESSAGE, Toast.LENGTH_LONG).show();
}
}
};
class MyBinder extends Binder{
public int getCount(){
return count;
}
}
}
2. MyBootReceiver
public class MyBootReceiver extends BroadcastReceiver {
static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
//扩展一个广播类,用于开机就启动服务
if (intent.getAction().equals(BOOT_COMPLETED)) {
Log.i("MyBootReceiver", "yzf, onReceive, startService");
Intent i = new Intent(context, MyService.class);
context.startService(i);
}
}
}
3. MainActivity
public class MainActivity extends Activity implements OnClickListener {
private Button btnOk;
private Button btnCancel;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MainActivity", "yzf, onCreate");
btnOk = (Button) findViewById(R.id.btn_ok);
btnCancel = (Button) findViewById(R.id.btn_cancel);
btnOk.setOnClickListener(this);
btnCancel.setOnClickListener(this);
//启动服务
intent = new Intent(this, MyService.class);
startService(intent);
}
//用以发送广播
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent vIntent;
switch(arg0.getId()){
case R.id.btn_ok:
vIntent = new Intent("send.ok.message");
sendBroadcast(vIntent);
break;
case R.id.btn_cancel:
vIntent = new Intent("send.cancle.message");
sendBroadcast(vIntent);
break;
}
}
public void onDestroy(){
super.onDestroy();
if(intent != null){
stopService(intent);
}
}
}
4. 服务与广播的XML注册
<service android:name=".MyService">
<intent-filter>
<action android:name="cn.m15.xys.MyService"></action>
</intent-filter>
<intent-filter>
<action android:name="send.ok.message" />
<action android:name="send.cancle.message" />
</intent-filter>
</service>
<receiver android:name=".MyBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>