bind开启服务

bind开启的服务可以掉服务里面的方法
特点是和Activity共死
服务都需要在清单中注册
我还在布局中添加了四个按钮的点击

package com.example.bindservice;

import com.example.bindservice.BindSevice.My;

import android.app.Activity;
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.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {
    private My my;
    private Conn conn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void start(View v){
        Intent intent=new  Intent(this,BindSevice.class);
        conn = new Conn();
        //bindService开启服务
        //参数1:intent,包含要启动哪一个Service
        //参数2:ServiceConnection接口,通过它可以接受服务开启或停止的消息
        //参数3:开启服务时操作的选项是,一般传入BIND_AUTO_CREATE自动创建Service
        bindService(intent, conn, BIND_AUTO_CREATE);
    
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unbindService(conn);//共死
    }
public void unbind(View v){
    unbindService(conn);
    }
public void Service(View v){
    my.showToast2("我去玩了");
}
class Conn implements ServiceConnection{

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        // TODO Auto-generated method stub
        Log.e("tag","onServiceConnected");//IBinder返回非null时走这个方法
        my = (My) service;
    }
    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub
        //服务正常退出后不会掉用此方法
        Log.e("tag","onServiceDisconnected");//链接断开,服务被销毁掉
    }
    
}
}

可以通过局部内部类调用了服务类的方法,如果服务类通过IBinder将return new My();出去,不为null时在Activity中的Conn局部内部类的onServiceConnected方法将会执行

服务类可以写

package com.example.bindservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class BindSevice extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        Log.e("tag","IBinder");
        return new My();
    }
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.e("tag","onCreate");
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Log.e("tag","onDestroy");
        super.onDestroy();
        
    }
    public void showToast(String s){
        Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
    }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Log.e("tag","onStartCommand");
    return super.onStartCommand(intent, flags, startId);

}
public class My extends Binder{
    public void showToast2(String s){
        showToast(s);
    }
}
}

效果是:我可以开启服务然后调用服务里面的吐司方法,

你可能感兴趣的:(bind开启服务)