zoukankan      html  css  js  c++  java
  • android service 组件

    Service概念及用途:

    Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果我们退出应用时,Service进程并没有结束,它仍然在后台运行,那 我们什么时候会用到Service呢?比如我们播放音乐的时候,有可能想边听音乐边干些其他事情,当我们退出播放音乐的应用,如果不用Service,我 们就听不到歌了,所以这时候就得用到Service了,又比如当我们一个应用的数据是通过网络获取的,不同时间(一段时间)的数据是不同的这时候我们可以 Service在后台定时更新,而不用每打开应用的时候在去获取。

    Service生命周期 :

    Android Service的生命周期并不像Activity那么复杂,它只继承了onCreate(),onStart(),onDestroy()三个方法,当我们第一次启动Service时,先后调用了onCreate(),onStart()这两个方法,当停止Service时,则执行onDestroy()方法,这里需要注意的是,如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行onStart()方法,具体的可以看下面的实例。

    ServiceActivity通信:

    Service后端的数据最终还是要呈现在前端Activity之上的,因为启动Service时,系统会重新开启一个新的进程,这就涉及到不同进程间通信的问题了(AIDL)这一节我不作过多描述,当我们想获取启动的Service实例时,我们可以用到bindServiceonBindService方法,它们分别执行了ServiceIBinder()onUnbind()方法。

    Service的类型

     

    Service有两种类型:

    1. 本地服务(Local Service):用于应用程序内部

    2. 远程服务(Remote Sercie):用于android系统内部的应用程序之间

    前者用于实现应用程序自己的一些耗时任务,比如查询升级信息,并不占用应用程序比如Activity所属线程,而是单开线程后台执行,这样用户体验比较好。

    后者可被其他应用程序复用,比如天气预报服务,其他应用程序不需要再写这样的服务,调用已有的即可。

    实例一

    演示如何创建、启动、停止及绑定一个service

    程序文件

    /Chapter07_Service_Example/src/com/amaker/ch07/app/MainActivity.java

    代码
    package com.amaker.ch07.app;

    import com.amaker.ch07.app.R;

    import android.app.Activity;
    import android.app.Service;
    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.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    /**
    * 测试Service
    */

    public class MainActivity extends Activity {
    // 声明Button
    private Button startBtn,stopBtn,bindBtn,unbindBtn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置当前布局视图
    setContentView(R.layout.main);
    // 实例化Button
    startBtn = (Button)findViewById(R.id.startButton01);
    stopBtn
    = (Button)findViewById(R.id.stopButton02);
    bindBtn
    = (Button)findViewById(R.id.bindButton03);
    unbindBtn
    = (Button)findViewById(R.id.unbindButton04);

    // 添加监听器
    startBtn.setOnClickListener(startListener);
    stopBtn.setOnClickListener(stopListener);
    bindBtn.setOnClickListener(bindListener);
    unbindBtn.setOnClickListener(unBindListener);

    }
    // 启动Service监听器
    private OnClickListener startListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
    // 创建Intent
    Intent intent = new Intent();
    // 设置Action属性
    intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");
    // 启动该Service
    startService(intent);
    }
    };

    // 停止Service监听器
    private OnClickListener stopListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
    // 创建Intent
    Intent intent = new Intent();
    // 设置Action属性
    intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");
    // 启动该Service
    stopService(intent);
    }
    };

    // 连接对象
    private ServiceConnection conn = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
    Log.i(
    "SERVICE", "连接成功!");
    Toast.makeText(MainActivity.
    this, "连接成功!", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onServiceDisconnected(ComponentName name) {
    Log.i(
    "SERVICE", "断开连接!");
    Toast.makeText(MainActivity.
    this, "断开连接!", Toast.LENGTH_LONG).show();
    }
    };

    // 綁定Service监听器
    private OnClickListener bindListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
    // 创建Intent
    Intent intent = new Intent();
    // 设置Action属性
    intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");

    // 绑定Service
    bindService(intent, conn, Service.BIND_AUTO_CREATE);
    }
    };


    // 解除绑定Service监听器
    private OnClickListener unBindListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
    // 创建Intent
    Intent intent = new Intent();
    // 设置Action属性
    intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");
    // 解除绑定Service
    unbindService(conn);
    }
    };

    }

     /Chapter07_Service_Example/src/com/amaker/ch07/app/MyService.java

    代码
    package com.amaker.ch07.app;

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

    /**
    * 测试Service
    */
    public class MyService extends Service{

    // 可以返回null,通常返回一个有aidl定义的接口
    public IBinder onBind(Intent intent) {
    Log.i(
    "SERVICE", "onBind..............");
    Toast.makeText(MyService.
    this, "onBind..............", Toast.LENGTH_LONG).show();
    return null;
    }
    // Service创建时调用
    public void onCreate() {
    Log.i(
    "SERVICE", "onCreate..............");
    Toast.makeText(MyService.
    this, "onCreate..............", Toast.LENGTH_LONG).show();
    }
    // 当客户端调用startService()方法启动Service时,该方法被调用
    public void onStart(Intent intent, int startId) {
    Log.i(
    "SERVICE", "onStart..............");
    Toast.makeText(MyService.
    this, "onStart..............", Toast.LENGTH_LONG).show();
    }
    // 当Service不再使用时调用
    public void onDestroy() {
    Log.i(
    "SERVICE", "onDestroy..............");
    Toast.makeText(MyService.
    this, "onDestroy..............", Toast.LENGTH_LONG).show();
    }
    }

    布局文件

    /Chapter07_Service_Example/res/layout/main.xml

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
    ="vertical"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    >

    <Button
    android:text="启动Service"
    android:id
    ="@+id/startButton01"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"></Button>

    <Button
    android:text="停止Service"
    android:id
    ="@+id/stopButton02"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"></Button>

    <Button
    android:text="绑定Service"
    android:id
    ="@+id/bindButton03"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"></Button>

    <Button
    android:text="解除绑定"
    android:id
    ="@+id/unbindButton04"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"></Button>


    </LinearLayout>

    清单文件

    /Chapter07_Service_Example/AndroidManifest.xml

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
    ="com.amaker.ch07.app"
    android:versionCode
    ="1"
    android:versionName
    ="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">

    <activity android:name=".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">
    <intent-filter>
    <action android:name="com.amaker.ch07.app.action.MY_SERVICE"/>
    </intent-filter>
    </service>

    </application>
    <uses-sdk android:minSdkVersion="3" />

    </manifest>

    实例二、远程service调用

    实现方式RPC(remote procedures call)远程进程调用      (android interface definition)接口定义语言

    /Chapter07_Service_Remote/src/com/amaker/ch07/app/MainActivity.java

    代码
    package com.amaker.ch07.app;

    import com.amaker.ch07.app.IPerson;
    import com.amaker.ch07.app.R;

    import android.app.Activity;
    import android.app.Service;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.os.RemoteException;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    /**
    *
    * RPC 测试
    */
    public class MainActivity extends Activity {
    // 声明IPerson接口
    private IPerson iPerson;
    // 声明 Button
    private Button btn;
    // 实例化ServiceConnection
    private ServiceConnection conn = new ServiceConnection() {
    @Override
    synchronized public void onServiceConnected(ComponentName name, IBinder service) {
    // 获得IPerson接口
    iPerson = IPerson.Stub.asInterface(service);
    if (iPerson != null)
    try {
    // RPC 方法调用
    iPerson.setName("hz.guo");
    iPerson.setAge(
    30);
    String msg
    = iPerson.display();
    // 显示方法调用返回值
    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG)
    .show();
    }
    catch (RemoteException e) {
    e.printStackTrace();
    }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置当前视图布局
    setContentView(R.layout.main);
    // 实例化Button
    btn = (Button) findViewById(R.id.Button01);
    //为Button添加单击事件监听器
    btn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    // 实例化Intent
    Intent intent = new Intent();
    // 设置Intent Action 属性
    intent
    .setAction(
    "com.amaker.ch09.app.action.MY_REMOTE_SERVICE");
    // 绑定服务
    bindService(intent, conn, Service.BIND_AUTO_CREATE);
    }
    });
    }
    }

    /Chapter07_Service_Remote/src/com/amaker/ch07/app/MyRemoteService.java

    代码
    package com.amaker.ch07.app;

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

    import com.amaker.ch07.app.IPerson.Stub;

    /**
    * 使用Service将接口暴露给客户端
    */
    public class MyRemoteService extends Service{
    // 声明IPerson接口
    private Stub iPerson = new IPersonImpl();
    @Override
    public IBinder onBind(Intent intent) {
    return iPerson;
    }
    }

    /Chapter07_Service_Remote/src/com/amaker/ch07/app/IPersonImpl.java

    代码
    package com.amaker.ch07.app;

    import com.amaker.ch07.app.IPerson;

    import android.os.RemoteException;
    /**
    *
    * IPerson接口实现类
    */
    public class IPersonImpl extends IPerson.Stub{
    // 声明两个变量
    private int age;
    private String name;
    @Override
    // 显示name和age
    public String display() throws RemoteException {
    return "name:"+name+";age="+age;
    }
    @Override
    // 设置age
    public void setAge(int age) throws RemoteException {
    this.age = age;
    }
    @Override
    // 设置name
    public void setName(String name) throws RemoteException {
    this.name = name;
    }
    }

    /Chapter07_Service_Remote/src/com/amaker/ch07/app/IPerson.aidl

    package com.amaker.ch07.app;

    interface IPerson {
    void setAge(int age);
    void setName(String name);
    String display();
    }

    布局文件

    /Chapter07_Service_Remote/res/layout/main.xml

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
    ="vertical"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    >

    <Button
    android:text="测试远程Service"
    android:id
    ="@+id/Button01"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"></Button>

    </LinearLayout>

    清单文件

    /Chapter07_Service_Remote/AndroidManifest.xml

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
    ="com.amaker.ch07.app"
    android:versionCode
    ="1"
    android:versionName
    ="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".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="MyRemoteService">
    <intent-filter>
    <action android:name="com.amaker.ch07.app.action.MY_REMOTE_SERVICE"/>
    </intent-filter>
    </service>

    </application>
    <uses-sdk android:minSdkVersion="3" />

    </manifest>
  • 相关阅读:
    php代码中注释的含义
    MySql-count(*)与count(id)与count(字段)之间的执行结果和性能分析
    mysql通配符进行模糊查询
    我的娃,我的宝贝
    Yii 2.0 query模式语法
    Yii 2.0版本调试输出SQL语句
    mysql高效率随机获取n条数据写法
    mysql语句中判断是否包含某字符串的方法
    应该让老婆多休息
    win10常用快捷键总结
  • 原文地址:https://www.cnblogs.com/linzheng/p/1941998.html
Copyright © 2011-2022 走看看