Android Service 组件

简介:
Service是Androd系统提供的四种组件之一,它的地位和Activity是并列的,只不过没有Activity的使用频率高。顾名思义Service就是运行在后台的一种服务程序,一般很少和用户交互,因此没有可视化界面。

下面我们演示一下如何创建一个Service:
1:我们通过布局文件layout/main.xml创建一个启动、停止、及绑定一个Service
<?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:id="@+id/startButton01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="启动Service"
  />
  <Button
    android:id="@+id/stopButton02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="停止Service"
  />
  <Button
    android:id="@+id/bindButton03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="绑定Service"
  />
  <Button
    android:id="@+id/unbindButton04"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="解除绑定"
  />
</LinearLayout>
2:我们创建一个MainActivity.java来实现Service
package com.android.test;

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;

public  class MainActivity  extends Activity {
     
   private Button startBtn, stopBtn, bindBtn, unbindBtn;
  
         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() {

     public  void onClick(View v) {
      Intent intent =  new Intent();
       //设置Action属性
      intent.setAction( "com.android.test.action.MY_SERVICE");
      startService(intent);
    }
          
        };
         //停止Service监听器
         private OnClickListener stopListener =  new OnClickListener() {

     public  void onClick(View v) {
      Intent intent =  new Intent();
      intent.setAction( "com.android.test.action.MY_SERVICE");
      stopService(intent);
    }
        };
         //绑定Service监听器
         private OnClickListener bindListener =  new OnClickListener() {

     public  void onClick(View v) {
      Intent intent =  new Intent();
      intent.setAction( "com.android.test.action.MY_SERVICE");
       //绑定Service
      bindService(intent, conn, Service.BIND_AUTO_CREATE);
    }
        };
         //解除Service监听器
         private OnClickListener unBindListener =  new OnClickListener() {

     public  void onClick(View v) {
      Intent intent =  new Intent();
      intent.setAction( "com.android.test.action.MY_SERVICE");
       //解除绑定
      unbindService(conn);
    }
        };
        
         private ServiceConnection conn =  new ServiceConnection() {

     public  void onServiceConnected(ComponentName name, IBinder service) {
      Log.i( "SERVICE""连接成功!");
      Toast.makeText(MainActivity. this"连接成功!", Toast.LENGTH_LONG).show();
    }

     public  void onServiceDisconnected(ComponentName name) {
      Log.i( "SERVICE""断开连接!");
      Toast.makeText(MainActivity. this"断开连接!", Toast.LENGTH_LONG).show();
    }      
        };
3:创建一个MyService.java继承Service,覆盖其声明周期中的方法,并在各个方法中显示信息
package com.android.test;

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

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......");
  }
   //当客户端调用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();
  }
}
4:在AndroidManifest.xml配置文件中声明Activity和Service
<?xml version= "1.0" encoding= "utf-8"?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
            package="com.android.test"
            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.android.test.action.MY_SERVICE"/>
      </intent-filter>
    </service>
        </application>
        <uses-sdk android:minSdkVersion="4" />

</manifest>
运行如图:









本文转自 Art_Hero 51CTO博客,原文链接:http://blog.51cto.com/curran/527777,如需转载请自行联系原作者
目录
相关文章
|
2月前
|
设计模式 Android开发
[Android 四大组件] --- BroadcastReceiver
[Android 四大组件] --- BroadcastReceiver
33 0
|
3月前
|
Android开发 开发者
什么是Android Jetpack,它包括哪些组件?
什么是Android Jetpack,它包括哪些组件?
41 0
|
4月前
|
数据库 Android开发
Android Studio开发之应用组件Application的讲解及实战(附源码,通过图书管理信息系统实战)
Android Studio开发之应用组件Application的讲解及实战(附源码,通过图书管理信息系统实战)
49 0
|
4月前
|
XML Java Android开发
Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信)
Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信)
82 0
|
4月前
|
XML Java Android开发
Android Studio App开发之服务Service的讲解及实战(包括启动和停止,绑定与解绑,推送服务到前台实现音乐播放器,附源码)
Android Studio App开发之服务Service的讲解及实战(包括启动和停止,绑定与解绑,推送服务到前台实现音乐播放器,附源码)
105 0
|
12天前
|
存储 数据库 Android开发
构建高效安卓应用:采用Jetpack架构组件优化用户体验
【4月更文挑战第12天】 在当今快速发展的数字时代,Android 应用程序的流畅性与响应速度对用户满意度至关重要。为提高应用性能并降低维护成本,开发者需寻求先进的技术解决方案。本文将探讨如何利用 Android Jetpack 中的架构组件 — 如 LiveData、ViewModel 和 Room — 来构建高质量的安卓应用。通过具体实施案例分析,我们将展示这些组件如何协同工作以实现数据持久化、界面与逻辑分离,以及确保数据的即时更新,从而优化用户体验并提升应用的可维护性和可测试性。
|
2月前
|
数据可视化 Android开发
[Android 四大组件] --- Service
[Android 四大组件] --- Service
24 0
|
2月前
|
Android开发
[Android 四大组件] --- Activity
[Android 四大组件] --- Activity
22 1
|
2月前
|
存储 数据库 Android开发
安卓四大组件是什么?
安卓四大组件是什么?
|
3月前
|
数据库 Android开发 开发者
Android基础知识:什么是Android应用的四大组件?
Android基础知识:什么是Android应用的四大组件?
62 1