Android service服务、aidl接口demo

Sercive 后台长期运行的组件
startService();//开启服务
stopService();//停止服务
bindService()绑定服务 可以得到服务的代理对象,间接调用服务路面的方法。
绑定服务:间接调用服务里面的方法;
 如果调用者activity被销毁了,服务也会跟着销毁(不求同时生,但求同时挂)
开启服务:不可以调用服务里面的方法;
如果调用者activity退出了,服务还会长期的在后台运行
生命周期:
1.单独调用:startService() onCreate
stopService() onDestroy
2.绑定调用:bind onCreate onBind
unBind onunbind onDestroy
服务只能被解绑一次,多次的解除解绑服务,服务会报错.
3.混合调用:
需求:既要保证服务长期在后台运行,又想去调用服务里面的方法。
技巧:1.先开启服务 2.绑定服务
执行步骤:1.开启服务 startService() onCreate
2.绑定服务 bindService() onBind()
3.关闭程序,调用者退出,服务被解绑
4.stopService()停止服务

服务流程图:


--------保证服务长期在后台运行,又想去调用服务里面的方法---
1.StartService()开启服务(保证服务在后台长期运行)
2.bindService()绑定服务(获取中间人,间接的调用服务里面的方法)
3.unBinderService()解除绑定服务,失去跟服务的连接,activity关闭 服务会执行onUnbinde方法
4.服务会长期在后台运行
--------------------------------
如果服务已经绑定过了,直接利用stopService()停不掉服务的 必须要显示的解除绑定服务 服务才能被停掉


如果服务没有通过startService()开人过,只要解除绑定服务 服务就会onDertory();
-----------------------
服务只能被解绑一次,多次解除绑定服务 应用程序会抛出异常。


------------ 绑定本地服务的流程 ---------------
1.在activity调用bindService()去绑定服务
bingService(intent, new MyConn(),BIND_AUTO_CREATE);
需要传递一个叫ServiceConntion的接口参数,用来返回两个回调,当服务被成功绑定,当服务失去连接


2.在服务里面需要重写方法onBind()在服务被绑定的时候调用返回 一个IBinder接口对象(代理人)
必须要实现一个方法,这个方法可以调用到服务的方法
3.在activity的onServiceConnected得到中间人
4.调用中间人的方法
5.中间人调用服务的方法

------------ 绑定远程服务的流程 aidl ---------------
1.在activity调用bindService()去绑定服务
bingService(intent. new MyConn(),BIND_AUTO_CREATE);
需要传递一个叫ServiceConntion的接口参数,用来返回两个回调,当服务被成功绑定,当服务失去连接
2.在服务里面需要重写方法onBind()在服务被绑定的时候调用返回 一个IBinder接口对象(代理人)接口定义需要改成aidl 用自动生成的IService.stub代理人
必须要实现一个方法,这个方法可以调用到服务的方法
3.在activity的onServiceConnected得到中间人 使用aidl自动生成IService 利用IService.stub.asInterface();
4.调用中间人的方法

5.中间人调用服务的方法

package com.example.service;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends Activity {

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

	public void click(View view) {
		Intent intent = new Intent(this, MyService.class);
		//开启服务
		startService(intent);
	}

	public void click1(View view) {
		Intent intent = new Intent(this, MyService.class);
		//停止服务
		stopService(intent);
	}

	public void click2(View view) {
		Intent intent = new Intent(this, MyService.class);
		if(con == null)
			con = new MyServiceConnection();
		//intent 激活代理人服务
		//代理人对象用来跟服务建立连接,不能为空
		//BIND_AUTO_CREATE 绑定服务方式,如果绑定是服务未启动,则先启动服务在绑定
		bindService(intent, con, BIND_AUTO_CREATE);
	}

	public void click3(View view) {
		//调用服务中的方法
		con.changService();
	}
	public void click4(View view) {
		try{
			//解绑服务
			unbindService(con);
		}catch(Exception e){
		}
	}
}

package com.example.service;

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

public class MyService extends Service {
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("---------onBind");
		return new MyBinder();
	}
	public class MyBinder extends Binder{
		public void change(){
			changService();
		}
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		System.out.println("---------onCreate");
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		System.out.println("---------onDestroy");
	}

	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("---------onUnbind");
		return super.onUnbind(intent);
	}
	
	public void changService(){
		System.out.println(",,,,,,,,,服务被调用了,,,,");
	}
}

package com.example.service;

import com.example.service.MyService.MyBinder;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;

public class MyServiceConnection implements ServiceConnection {
	MyBinder binder;
	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		// TODO Auto-generated method stub
		System.out.println("*********"+service);
		binder = (MyBinder) service;
		System.out.println("---------onServiceConnected");
	}

	@Override
	public void onServiceDisconnected(ComponentName name) {
		// TODO Auto-generated method stub
		System.out.println("---------onServiceDisconnected");
	}
	
	public void changService(){
		System.out.println("调用的改变服务**");
		binder.change();
	}

}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.service"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.service.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyService"></service>
    </application>

</manifest>

点击下载源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值