2、新建MyBroadcastreceiver类,继承BroadcastReceiver,用来发送Intent启动服务
package com.ljq.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* 发送Intent启动服务
*
* @author jiqinlin
*
*/
public class MyBroadcastreceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, MyService.class);
context.startService(service);
}
}
3、新建MainActivity类,其实是一个activity,用来呈现界面
package com.ljq.activity;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
private String msg = "";
private TextView txtMsg;
private UpdateReceiver receiver;
private MyService myService;
private final static String TAG=MainActivity.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtMsg = (TextView) this.findViewById(R.id.txtMsg);
this.findViewById(R.id.btnStart).setOnClickListener(this);
this.findViewById(R.id.btnSend).setOnClickListener(this);
//订阅广播Intent
receiver = new UpdateReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.android.Yao.msg");
this.registerReceiver(receiver, filter);
//初始化时启动服务
//Intent intent = new Intent(MainActivity.this, MyService.class);
//this.bindService(intent, conn, BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
//结束服务
if(conn!=null){
unbindService(conn);
myService=null;
}
}
public class UpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取service传过来的信息
msg = intent.getStringExtra("msg");
txtMsg.append(msg);
}
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService = ((MyService.LocalBinder) service).getService();
Log.i(TAG, "onServiceConnected myService: "+myService);
}
@Override
public void onServiceDisconnected(ComponentName name) {
myService = null;
}
};
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyService.class);
switch (v.getId()) {
case R.id.btnStart:
//判断服务是否启动
if(false==isServiceRunning(this, MyService.class.getName())){
Log.i(TAG, "start "+MyService.class.getSimpleName()+" service");
this.bindService(intent, conn, BIND_AUTO_CREATE);
}
Log.i(TAG, MyService.class.getName()+" run status: "+isServiceRunning(this, MyService.class.getName()));
break;
case R.id.btnSend:
//判断服务是否启动
if(false==isServiceRunning(this, MyService.class.getName())){
Log.i(TAG, "start "+MyService.class.getSimpleName()+" service");
this.bindService(intent, conn, BIND_AUTO_CREATE);
}
Log.i(TAG, MyService.class.getName()+" run status: "+isServiceRunning(this, MyService.class.getName()));
Log.i(TAG, "onClick myService: "+myService); //第一次启动服务时此处为null(小编认为虽然服务已启动成功,但是还没全部初始化)
if(myService!=null){
myService.sendMsgtoServer("i am sending msg to server");
//从activity传递信息给service
myService.receiverMsgtoActivity("this is a msg");
}
break;
}
}
/**
* 判断服务是否正在运行
*
* @param context
* @param className 判断的服务名字:包名+类名
* @return true在运行 false 不在运行
*/
public static boolean isServiceRunning(Context context, String className) {
boolean isRunning = false;
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
//获取所有的服务
List<ActivityManager.RunningServiceInfo> services= activityManager.getRunningServices(Integer.MAX_VALUE);
if(services!=null&&services.size()>0){
for(ActivityManager.RunningServiceInfo service : services){
if(className.equals(service.service.getClassName())){
isRunning=true;
break;
}
}
}
return isRunning;
}
}










