Service1

原来 service 是指界面被关掉 还能继续运行的意思,运行在后台。

startservice
stopservice

绑定服务(关闭界面,会退出当前的app)

bindservice
unbindservice

基本用法如下:
package zhbit.test;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Using_ServiceActivity extends Activity implements OnClickListener, ServiceConnection {
/* Called when the activity is first created. /
private Button btnstartservice, btnstopservice, btnbindservice,
btnunbindservice;
private Intent intent1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    intent1 = new Intent(Using_ServiceActivity.this, Second.class);

    btnstartservice = (Button) findViewById(R.id.StartService);
    btnstopservice = (Button) findViewById(R.id.stopService);
    btnbindservice = (Button) findViewById(R.id.BindService);
    btnunbindservice = (Button) findViewById(R.id.UnBindService);
    btnstartservice.setOnClickListener(this);
    btnstopservice.setOnClickListener(this);
    btnbindservice.setOnClickListener(this);
    btnunbindservice.setOnClickListener(this);

}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.StartService:
        startService(intent1);
        break;
    case R.id.stopService:
        stopService(intent1);
        break;
    case R.id.BindService:
        bindService(intent1, this, Context.BIND_AUTO_CREATE);
        break;
    case R.id.UnBindService:
        unbindService(this);
    //  stopService(intent1);
        break;

    default:
        break;
    }
}

public void onServiceConnected(ComponentName name, IBinder service) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "onserviceconnected", Toast.LENGTH_LONG).show();
}

public void onServiceDisconnected(ComponentName name) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "onservicedisconnected", Toast.LENGTH_LONG).show();
}

}


package zhbit.test;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class Second extends Service{

@Override
public void onCreate() {

    Toast.makeText(this, "onCreate", Toast.LENGTH_LONG).show();
    System.out.print("onCrate");
    super.onCreate();
}

@Override
public void onDestroy() {
    Toast.makeText(this, "onDestroy", Toast.LENGTH_LONG).show();
    System.out.print("onDestroy");
    super.onDestroy();
}
private final EchServiceBinder echservicebinder = new EchServiceBinder();
public class EchServiceBinder extends Binder{

}
@Override
public IBinder onBind(Intent intent) {
    Toast.makeText(this, "onBind", Toast.LENGTH_LONG).show();
    return echservicebinder;
}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值