Android基础知识_使用Service
2015-06-30 20:40
441 查看
一Service概述
二使用Service的示例
三程序运行描述
A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
Activity可以呈现界面与用户进行交互,在很多时候程序不需要有与用户交互的界面, 只需要一直在后台运行着做一些事务的处理。比如说socket常连接、HTTP的网络通信以及与服务器保持着推送的连接,这些都会使用到Service。
示例工程LearnServiceDemo activity_main.xml布局文件的代码如下:
示例工程LearnServiceDemo MainActivity.java的代码如下:
示例工程LearnServiceDemo AndroidManifest.xml的部分主要代码如下:
二使用Service的示例
三程序运行描述
一、Service概述
API中关于Service的描述如下:A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
Activity可以呈现界面与用户进行交互,在很多时候程序不需要有与用户交互的界面, 只需要一直在后台运行着做一些事务的处理。比如说socket常连接、HTTP的网络通信以及与服务器保持着推送的连接,这些都会使用到Service。
二、使用Service的示例
示例工程LearnServiceDemo MyService.java的代码如下(被注释的代码块解除之后,运行程序能够观察出程序在后台如何运行。步骤:点击启动服务,在LogCat中可以看到每隔一秒输出“服务正在运行…”,退出Activity服务仍然继续运行。):package com.example.learnservice; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { public MyService() { } @Override public IBinder onBind(Intent intent) { return null; } // /* 在外界执行了startService之后,会执行到onStartCommand */ // @Override // public int onStartCommand(Intent intent, int flags, int startId) { // /* 创建线程不断运行输出语句 */ // new Thread() { // public void run() { // super.run(); // /* 该死循环的作用是观察程序在后台的运行, 仅用来测试 // * 效果: 当服务启动后, 每隔一秒输出 服务正在运行... // * */ // while (true) { // System.out.println("服务正在运行..."); // try { // sleep(1000); // } catch (Exception e) { // e.printStackTrace(); // } // } // }; // }.start(); // return super.onStartCommand(intent, flags, startId); // } }
示例工程LearnServiceDemo activity_main.xml布局文件的代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/btnStartService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动服务" /> <Button android:id="@+id/btnStopService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止服务" /> </LinearLayout>
示例工程LearnServiceDemo MainActivity.java的代码如下:
package com.example.learnservice; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity { private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 服务的实例在操作系统中只有一个 * Intent用来配置程序要启动的Service信息 * 故定义一个成员变量intent, 便于startService、stopService使用 * */ intent = new Intent(MainActivity.this, MyService.class); /* 添加事件监听器, 启动服务 */ findViewById(R.id.btnStartService).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /* 启动服务 */ startService(intent); } }); /* 添加事件监听器, 停止服务 */ findViewById(R.id.btnStopService).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { /* 停止服务 */ stopService(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
示例工程LearnServiceDemo AndroidManifest.xml的部分主要代码如下:
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.learnservice.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" android:enabled="true" android:exported="true" > </service> </application>
三、程序运行描述
通过模拟器运行程序,先不对本程序进行操作。而先打开Settings(设置),点击Apps(应用),选择RUNNING(正在运行),这里是所有正在运行的程序和服务,可以看到该程序并没有运行;打开LearnServiceDemo点击启动服务,再回到Settings(设置),可以看到多了一个Service,有一个进程和一个服务;再回到LearnServiceDemo点击停止服务,之后回到Settings(设置),可以看到服务已经没有了。通过这种方式我们可以把我们的程序运行在后台。相关文章推荐
- android菜鸟学习笔记11----Intent的两点补充
- Android按钮单击事件的四种常用写法总结
- Android studio 关联源代码
- android 学习笔记-1
- android编译错误:No such file or directory
- Android内核开发:浅析APK的安装过程
- Android内核开发:浅析APK的安装过程
- android 完美退出应用程序。
- Android监听程序自身被卸载
- Android监听程序自身被卸载
- 修正Android基于ZXing的二维码扫描——横竖屏自由切换
- 修正Android基于ZXing的二维码扫描——横竖屏自由切换
- android在线程(Thread)里面弹Toast框
- 关于Android加载html代码出现的问题
- MTK6577+Android之音频(audio)移植
- android xfermode绘图
- Android SDK开发包国内下载地址
- Android Audio System
- Android JNI(实现自己的JNI_OnLoad函数)
- Android环境搭建 NDK+ADT(免cywgin)