求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   模型库  
会员   
 


DeepSeek大模型应用开发实践
6月12-13日 厦门



基于 UML 和EA进行分析设计
6月23-24日 北京+线上



人工智能、机器学习& TensorFlow+Keras
6月22-23日 北京
 
 
 

Android开发教程
Android 开发环境配置
Android 架构
Android 应用组件
Android Hello World示例
Android 资源组织和访问
Android Activity
Android Service
Android 广播接收器
Android 内容提供者
Android 碎片/片段
Android Intent过滤器
Android UI布局
Android UI控件
Android 事件处理
Android 样式和主题
Android 自定义组件
Android 拖放
Android 通知
Android 基于位置服务
Android 发送电子邮件
Android 发送短信/SMS
Android 拨打电话
发布Android应用
ndroid Alertdialog
Android Animation实例
Android音频捕获
Android音频管理器实例
Android
Android最佳实践
Android Bluetooth实例
Android Camera
Android Clipboard
Android自定义字体
Android数据备份
Android Gestures/手势
Android图片效果
Android图片切换
Android内部存储
Android JetPlayer实例
Android JSON解析器
Android加载Spinner
Android本地化
Android登录实例
Android MediaPlayer
 
 

Android Service
1109 次浏览
49次  

Service(服务)是一种在后台运行,执行长时间运行的操作,无需与用户交互的组件。例如,一个服务可以在后台播放音乐,用户在不同的应用程序或者可能通过网络获取数据,而不阻塞用户交互活动。本质上,一个服务可以采取两种状态:

每个服务都具有生命周期回调方法,可以实现监视服务的状态变化,并在适当的阶段执行工作。下图左侧显示的整个生命周期由StartService()创建提供服务 ,右边的图显示bindService()创建的整个生命周期提供服务:

要创建一个服务,需要创建一个Java类,扩展Service基类或者它的子类。Service基类定义各种回调方法,如下面表格给出。但是也并不需要实现所有的回调方法。重要的是要了解每一个变化以及实现,以确保应用程序能如用户所期望的行为方式运行。

下面的主服务演示每一个方法生命周期:

package com.yiibai;
    import android.app.Service;
  import android.os.IBinder;
  import android.content.Intent;
  import android.os.Bundle;
    public class HelloService extends Service {
          /** indicates how to behave if the service is killed */
     int mStartMode;
     /** interface for clients that bind */
     IBinder mBinder;
          /** indicates whether onRebind should be used */
     boolean mAllowRebind;
       /** Called when the service is being created. */
     @Override
     public void onCreate() {
            
   }
       /** The service is starting, due to a call to startService() */
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
        return mStartMode;
     }
       /** A client is binding to the service with bindService() */
     @Override
     public IBinder onBind(Intent intent) {
        return mBinder;
     }
       /** Called when all clients have unbound with unbindService() */
     @Override
     public boolean onUnbind(Intent intent) {
        return mAllowRebind;
     }
       /** Called when a client is binding to the service with bindService()*/
     @Override
     public void onRebind(Intent intent) {
       }
       /** Called when The service is no longer used and is being destroyed */
     @Override
     public void onDestroy() {
       }
  }

例子

这个例子将通过简单的步骤显示了如何创建Android服务。按照下面的步骤来修改前面章节创建的Android应用程序 - Hello World示例 :

以下是改性主要活动文件 src/com.example.helloworld/MainActivity.java 的内容。这个文件包括每个基本的生命周期方法。添加 StartService() 和 stopService() 方法来启动和停止服务。

package com.example.helloworld;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.content.Intent;
    import android.view.View;
       public class MainActivity extends Activity {
         @Override
         public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
      }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
       }
         // Method to start the service
       public void startService(View view) {
          startService(new Intent(getBaseContext(), MyService.class));
       }
       // Method to stop the service
        public void stopService(View view) {
        stopService(new Intent(getBaseContext(), MyService.class));
     }
  }

以下是src/com.example.helloworld/MyService.java 的内容。这个文件可以有一个或多个方法来使用服务。现在要实现只有两个方法 onStartCommand() 和 onDestroy() :

package com.example.helloworld;
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.widget.Toast;
    public class MyService extends Service {
       @Override
       public IBinder onBind(Intent arg0) {
          return null;
       }
       @Override
       public int onStartCommand(Intent intent, int flags, int startId) {
          // Let it continue running until it is stopped.
          Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
          return START_STICKY;
       }
       @Override
       public void onDestroy() {
         super.onDestroy();
         Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
      }
   }

下面将 AndroidManifest.xml 文件的内容修改。在这里添加 标签,包括服务:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.helloworld"
     android:versionCode="1"
     android:versionName="1.0" >
     <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
     <application
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/AppTheme" > 
        <activity 
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER"/>
             </intent-filter>
         </activity>
         <service android:name=".MyService" />
     </application>
  </manifest>

将以下是 res/layout/activity_main.xml 文件的内容,包括两个按钮:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical" >
       <Button android:id="@+id/btnStartService"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/start_service"
       android:onClick="startService"/>
       <Button android:id="@+id/btnStopService"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/stop_service"
       android:onClick="stopService" />
    </LinearLayout>

下面将在 res/values/strings.xml 中定义两个新的常量:

<resources>
      <string name="app_name">HelloWorld</string>
      <string name="hello_world">Hello world!</string>
      <string name="menu_settings">Settings</string>
      <string name="title_activity_main">MainActivity</string>
      <string name="start_service">Start Service</string>
      <string name="stop_service">Stop Service</string>
    </resources>

现在运行修改后的 Hello World!应用程序。假设创建了AVD 并同时做了环境设置。要在Eclipse运行的应用程序,打开一个项目的活动文件,从工具栏上找到并单击 “run”图标。 Eclipse AVD上安装的应用程序,并启动它,如果一切设置以及应用都没有问题,那么将会显示以下模拟器窗口:

要开始服务,现在就点击启动服务按钮,onStartCommand() 方法在程序中,每一个服务开始后将出现消息在模拟器底部,如下:

要停止该服务,可以点击停止服务(Stop Service)按钮。


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



1109 次浏览
49次
 捐助