Service启动之启动方式和绑定方式

AndroidManifest.xml




    
        
            
                

                
            
        
        
    

MainActivity.java

package com.example.lpc.service;

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.View;
import android.widget.Toast;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //启动服务
    public void startMyService(View v) {
        Intent intent = new Intent(this, Myservice.class);
        startService(intent);
        Toast.makeText(this, "start service", Toast.LENGTH_SHORT).show();
    }

    public void stopMyService(View v) {
        Intent intent = new Intent(this, Myservice.class);
        stopService(intent);
        Toast.makeText(this, "stop service", Toast.LENGTH_SHORT).show();
    }

    private ServiceConnection conn;

    public void bindMyService(View v) {
        Intent intent = new Intent(this, Myservice.class);
        if (conn == null) {
            conn = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.e("ATG", "onServiceConnected");
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.e("ATG", "onServiceDisconnected");

                }
            };//实现一个接口 注意有逗号
        } else {
            Toast.makeText(this, "已经绑定", Toast.LENGTH_SHORT).show();
        }
        bindService(intent, conn, BIND_AUTO_CREATE);
    }

    public void unbindMyService(View v) {
        if (conn != null) {
            unbindService(conn);
            conn = null;
            Toast.makeText(this, "绑定", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "no绑定", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (conn != null) {
            unbindService(conn);
            conn = null;
        }
    }
}

MyService.java

package com.example.lpc.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/*
1. startService(intent)
	第一次调用 : -->构造方法()-->onCreate()-->onStartCommand()
	(重要)后面再调用 : -->onStartCommand()
	stopService() : -->onDestory()
2. bindService(intent, serviceConnection)
	调用 : -->构造方法()-->onCreate()-->onBind()-->onServiceConnected()
	unbindService(): (中有当前Activity与Service连接)-->onUnbind()-->onDestroy()

 */
public class Myservice extends Service {


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("TAG", "onBind()");
        return new Binder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("TAG", "onUnbind()");
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("TAG","onCreate()");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("TAG","onDestory()");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("TAG","onStarteCommand()");
        return super.onStartCommand(intent, flags, startId);

    }
}

activity_main.xml




    

    

        

你可能感兴趣的:(Android)