android绑定服务和开启服务,android-启动和绑定的服务何时被销毁?

是的,它有效。我想完成一个示例代码:

我必须使用一个由活动启动的服务来制作应用程序,该活动必须调用该服务中的某些方法,即使该活动被杀死,该服务也必须在后台运行,并且当该活动重新启动时,它就不必 如果服务正在运行,请重新启动它。 希望对您有所帮助,您可以了解它如何与日志一起使用。这就是代码:

public class MyActivity extends Activity{

private MyService myService;

private boolean mIsBound = false;

private ServiceConnection mConnection = new ServiceConnection() {

public void onServiceConnected(ComponentName className, IBinder binder) {

MyService.MyBinder b = (MyService.MyBinder) binder;

myService = b.getService();

mIsBound = true

//Do something

// Here you can call : myService.aFonctionInMyService();

}

public void onServiceDisconnected(ComponentName className) {

// Do something

mIsBound = false;

}

}

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

//Checked if my service is running

if (!isMyServiceRunning()) {

//if not, I start it.

startService(new Intent(this,MyService.class));

}

}

private boolean isMyServiceRunning() {

ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

for (RunningServiceInfo service : manager

.getRunningServices(Integer.MAX_VALUE)) {

if (MyService.class.getName().equals(

service.service.getClassName())) {

return true;

}

}

return false;

}

@Override

protected void onResume() {

// TODO Auto-generated method stub

super.onResume();

doBindService();

}

//Connection to the Service

private void doBindService() {

bindService(new Intent(this,MyService.class), mConnection,

Context.BIND_AUTO_CREATE);

}

// Disconnection from the service

private void doUnbindService() {

if (mIsBound) {

// Detach our existing connection.

unbindService(mConnection);

}

}

@Override

protected void onPause() {

// TODO Auto-generated method stub

doUnbindService();

super.onPause();

}

}

public class MyService extends Service{

public static String Tag = "MyService";

private final IBinder mBinder = new MyBinder();

@Override

public void onCreate() {

// TODO Auto-generated method stub

super.onCreate();

Log.d(Tag, "onCreate()");

}

public class MyBinder extends Binder {

public LocationService getService() {

return LocationService.this;

}

}

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

Log.d(Tag, "onBind()");

return mBinder;

}

@Override

public boolean onUnbind(Intent intent) {

// TODO Auto-generated method stub

Log.d(Tag, "onUnBind()");

return super.onUnbind(intent);

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

// TODO Auto-generated method stub

Log.d(Tag,"onStartCommand()");

return START_STICKY;

}

@Override

public void onDestroy() {

// TODO Auto-generated method stub

Log.d(Tag, "onDestroy");

super.onDestroy();

}

public void aFonctionInMyService(){

//Do Something

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值