- 浏览: 3513488 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (1491)
- Hibernate (28)
- spring (37)
- struts2 (19)
- jsp (12)
- servlet (2)
- mysql (24)
- tomcat (3)
- weblogic (1)
- ajax (36)
- jquery (47)
- html (43)
- JS (32)
- ibatis (0)
- DWR (3)
- EXTJS (43)
- Linux (15)
- Maven (3)
- python (8)
- 其他 (8)
- JAVASE (6)
- java javase string (0)
- JAVA 语法 (3)
- juddiv3 (15)
- Mule (1)
- jquery easyui (2)
- mule esb (1)
- java (644)
- log4j (4)
- weka (12)
- android (257)
- web services (4)
- PHP (1)
- 算法 (18)
- 数据结构 算法 (7)
- 数据挖掘 (4)
- 期刊 (6)
- 面试 (5)
- C++ (1)
- 论文 (10)
- 工作 (1)
- 数据结构 (6)
- JAVA配置 (1)
- JAVA垃圾回收 (2)
- SVM (13)
- web st (1)
- jvm (7)
- weka libsvm (1)
- weka屈伟 (1)
- job (2)
- 排序 算法 面试 (3)
- spss (2)
- 搜索引擎 (6)
- java 爬虫 (6)
- 分布式 (1)
- data ming (1)
- eclipse (6)
- 正则表达式 (1)
- 分词器 (2)
- 张孝祥 (1)
- solr (3)
- nutch (1)
- 爬虫 (4)
- lucene (3)
- 狗日的腾讯 (1)
- 我的收藏网址 (13)
- 网络 (1)
- java 数据结构 (22)
- ACM (7)
- jboss (0)
- 大纸 (10)
- maven2 (0)
- elipse (0)
- SVN使用 (2)
- office (1)
- .net (14)
- extjs4 (2)
- zhaopin (0)
- C (2)
- spring mvc (5)
- JPA (9)
- iphone (3)
- css (3)
- 前端框架 (2)
- jui (1)
- dwz (1)
- joomla (1)
- im (1)
- web (2)
- 1 (0)
- 移动UI (1)
- java (1)
- jsoup (1)
- 管理模板 (2)
- javajava (1)
- kali (7)
- 单片机 (1)
- 嵌入式 (1)
- mybatis (2)
- layui (7)
- asp (12)
- asp.net (1)
- sql (1)
- c# (4)
- andorid (1)
- 地价 (1)
- yihuo (1)
- oracle (1)
最新评论
-
endual:
https://blog.csdn.net/chenxbxh2 ...
IE6 bug -
ice86rain:
你好,ES跑起来了吗?我的在tomcat启动时卡在这里Hibe ...
ES架构技术介绍 -
TopLongMan:
...
java public ,protect,friendly,private的方法权限(转) -
贝塔ZQ:
java实现操作word中的表格内容,用插件实现的话,可以试试 ...
java 读取 doc poi读取word中的表格(转) -
ysj570440569:
Maven多模块spring + springMVC + JP ...
Spring+SpringMVC+JPA
Android开发中,当需要创建在后台运行的程序的时候,就要使用到Service。Service 可以分为有无限生命和有限生命两种。
特别需要注意的是Service跟Activities是不同的(简单来说可以理解为后台与前台的区别),例如,如果需要使用Service的话,需要调用startService(),从而利用startService()去调用Service中的OnCreate()和onStart()方法来启动一个后台的Service。
启动一个Service的过程如下:
context.startService() ->onCreate()- >onStart()->Service running
其中onCreate()可以进行一些服务的初始化工作,onStart()则启动服务。
停止一个Service的过程如下:
context.stopService() | ->onDestroy() ->Service stop
接下来的实例是一个利用后台服务播放音乐的小例子,点击start运行服务,点击stop停止服务。
程序运行界面:
代码:
定义服务,MyService.java
- package com.example;
- import android.app.Service;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.os.IBinder;
- import android.util.Log;
- import android.widget.Toast;
- public class MyService extends Service {
- private static final String TAG = "MyService";
- MediaPlayer player;
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
- Log.d(TAG, "onCreate");
- player = MediaPlayer.create(this, R.raw.braincandy);//运行例子是,需要替换音乐的名称
- player.setLooping(false); // Set looping
- }
- @Override
- public void onDestroy() {
- Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
- Log.d(TAG, "onDestroy");
- player.stop();
- }
- @Override
- public void onStart(Intent intent, int startid) {
- Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
- Log.d(TAG, "onStart");
- player.start();
- }
- }
除此之外还要在Manifest里面声明服务:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example" android:versionCode="1" android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".ServicesDemo" 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:enabled="true" android:name=".MyService" />
- </application>
- <uses-sdk android:minSdkVersion="3" />
- </manifest>
定义Activity,ServicesDemo.java
- package com.example;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class ServicesDemo extends Activity implements OnClickListener {
- private static final String TAG = "ServicesDemo";
- Button buttonStart, buttonStop;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- buttonStart = (Button) findViewById(R.id.buttonStart);
- buttonStop = (Button) findViewById(R.id.buttonStop);
- buttonStart.setOnClickListener(this);
- buttonStop.setOnClickListener(this);
- }
- public void onClick(View src) {
- switch (src.getId()) {
- case R.id.buttonStart:
- Log.d(TAG, "onClick: starting srvice");
- startService(new Intent(this, MyService.class));
- break;
- case R.id.buttonStop:
- Log.d(TAG, "onClick: stopping srvice");
- stopService(new Intent(this, MyService.class));
- break;
- }
- }
- }
- ServicesDemo.rar (26.5 KB)
- 下载次数: 955
发表评论
-
android离线
2019-11-04 21:25 271https://www.jianshu.com/p/fdfc4 ... -
android 异步更新
2015-07-11 22:42 1470http://www.apkbus.com/forum.php ... -
仿微米
2015-06-26 10:22 0http://www.javaapk.com/source/4 ... -
仿微信
2015-06-26 10:21 1886http://www.javaapk.com/source/1 ... -
网易客户端
2015-06-26 10:20 1570http://www.javaapk.com/source/4 ... -
仿朋友圈
2015-06-26 10:18 1677http://www.javaapk.com/source/8 ... -
仿朋友圈
2015-06-26 10:13 1792http://www.eoeandroid.com/threa ... -
全国ABC查询
2015-06-26 10:07 1298IM 功能 环信云 信息统计 Umeng 分享Umeng 更新 ... -
android 判断网络
2015-06-25 16:00 1230http://www.cnblogs.com/renqingp ... -
android 多彩表格
2015-05-31 16:06 1445http://blog.csdn.net/conowen/ar ... -
android socket
2015-05-31 16:01 1604http://blog.csdn.net/conowen/ar ... -
微信播放语音3
2015-02-25 12:00 1398http://download.csdn.net/detail ... -
微信语音播放2
2015-02-25 11:57 1307http://download.csdn.net/detail ... -
微信语音播放
2015-02-25 11:56 1652http://www.apkbus.com/android-1 ... -
超强的资料整合
2015-01-05 11:04 1200http://zz563143188.iteye.com/bl ... -
最强开发资源平台大合集
2015-01-04 14:50 1858程序员代码应用开发平 ... -
Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
2014-09-04 15:45 1340http://blog.csdn.net/vipzjyno1/ ... -
android应用推广
2014-09-03 23:25 1286应用推广不必愁,有米 ... -
android 图片异步加载
2014-09-03 16:47 1102http://www.csdn.net/article/201 ... -
android hander的使用
2014-09-02 23:46 1330http://www.cnblogs.com/xiaoran1 ...
相关推荐
android service 学习(下) ,共两部分
android service 学习笔记(上、下)android service 学习笔记(上、下)
android service 学习笔记(上、下)
android service 学习(上),共两部分
Service学习资料,包含Service的定义使用,两种启动方式。有两个demo,里面有介绍service基础知识,自定义Binder,如果与Activity通信。详情也可参考:http://blog.csdn.net/u012764110/article/details/48491567
小例子很实用,可以了解学习下Android强大的service功能 比如定时提醒,背景音乐播放等用service怎么实现。
NULL 博文链接:https://lichunan-d-163-com.iteye.com/blog/1568506
android学习之Service启动1
这是Pro Android学习系列中Android Service部分的例子源代码。相关学习笔记见:http://blog.csdn.net/flowingflying/article/details/6212512
android service 源码 学习
这个是学习Android Service时的一个练习!
用android的service做的音乐播放器,让service后台播放.mp3文件
这是自己学习android中service组件的代码,供以后查看用。
Android在开机启动后系统会发出一个Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED,这个Action只会发出一次。
分析了framework中battery相关知识。
Service学习时模仿别人实现的断电下载功能,使用Sqlite保持线程信息
这是我自己学习android service写的demo,详细介绍了创建service的步骤及用法。 还有android service与activity的交互操作。
Android之Service学习总结收集.pdf
NULL 博文链接:https://chaowen0668.iteye.com/blog/1090096
ZQYServiceDemo