zoukankan      html  css  js  c++  java
  • Service服务

    一.特点

       1.没有用户界面,在后台运行

       2.应用退出后,Service还继续运行

       3.默认情况下,在应用的主线程运行

       4.应用重新启动,可以继续调用前面启动的Service

    二.分类

       1.本地服务:Service对象和启动者在同一个进程内;进程内通信。

       2.远程服务:Service对象和启动者在不同的进程内;进程间通信,通过AIDL(Android接口定义语言 ) 来实现

    三.实现

       1.普通方式

       2.绑定方式

    普通方式的启动与停止代码展示:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.hanqi.testservice.MainActivity"
    11     android:orientation="vertical">
    12 
    13     <Button
    14         android:layout_width="match_parent"
    15         android:layout_height="wrap_content"
    16         android:text="普通方式启动"
    17         android:onClick="bt1_OnClick"/>
    18 
    19     <Button
    20         android:layout_width="match_parent"
    21         android:layout_height="wrap_content"
    22         android:text="普通方式停止"
    23         android:onClick="bt2_OnClick"/>
    24 </LinearLayout>
    .xml
     1 package com.hanqi.testservice;
     2 
     3 import android.content.Intent;
     4 import android.support.v7.app.AppCompatActivity;
     5 import android.os.Bundle;
     6 import android.view.View;
     7 import android.widget.Toast;
     8 
     9 public class MainActivity extends AppCompatActivity {
    10 
    11     @Override
    12     protected void onCreate(Bundle savedInstanceState) {
    13         super.onCreate(savedInstanceState);
    14         setContentView(R.layout.activity_main);
    15     }
    16 
    17     //普通方式启动
    18     public void bt1_OnClick(View v)
    19     {
    20         //准备Intent:显式意图
    21         Intent intent=new Intent(this,MyService.class);
    22         //启动MyService
    23         startService(intent);
    24 
    25         Toast.makeText(MainActivity.this, "MyService已启动", Toast.LENGTH_SHORT).show();
    26 
    27 
    28     }
    29 
    30     //普通方式停止
    31     public void bt2_OnClick(View v)
    32     {
    33         //准备Intent:显式意图
    34         Intent intent=new Intent(this,MyService.class);
    35         //启动MyService
    36         stopService(intent);
    37 
    38         Toast.makeText(MainActivity.this, "MyService已停止", Toast.LENGTH_SHORT).show();
    39 
    40 
    41     }
    42 }
    .java
     1 package com.hanqi.testservice;
     2 
     3 import android.app.Service;
     4 import android.content.Intent;
     5 import android.os.IBinder;
     6 import android.util.Log;
     7 
     8 public class MyService extends Service {
     9     public MyService() {
    10 
    11         Log.e("TAG","MyService被构造");
    12     }
    13 
    14 
    15     //回调方法
    16     //绑定
    17     @Override
    18     public IBinder onBind(Intent intent) {
    19         // TODO: Return the communication channel to the service.
    20         throw new UnsupportedOperationException("Not yet implemented");
    21     }
    22 }
    .service

     

  • 相关阅读:
    【转】异常处理模块
    【转】整套完整安全的API接口解决方案
    百度地图API功能集锦
    VS2015 使用Razor编写MVC视图时,Razor智能提示消失,报各种红线解决方案。
    算法初涉-解决比9*9数独更复杂的结构
    SQL时间相关
    ubuntu 安装
    dwa 设置多个目标点,倒车设计
    ros 信号周期的简单实现
    C++学习记录 第一章:初始
  • 原文地址:https://www.cnblogs.com/arxk/p/5595398.html
Copyright © 2011-2022 走看看