Android Service与Runnable整合并用
服务的启动没有Activity,即便是利用Activity带起服务,也会有各看成独立的事件及焦点要处理。
Service继承自Android.app.Service。
服务的生态链就先从onCreate()开始(如果有重写的话) ,接着应会进入启动服务onStart(),默认继承的Service类,并不一定要有onStart(),但是一定要重写public IBinder onBind(Intent intent)方法。
package cn.iimob;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class demo extends Activity {
private Button btnStartService,btnStopService;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStartService=(Button)findViewById(R.id.btnStartService);
btnStopService=(Button)findViewById(R.id.btnStopService);
btnStartService.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
//构建 Intent 对象,指定打开对象为 MyService服务
Intent i=new Intent(demo.this, MyService.class);
//设置新Task的方式
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//以startService 方法启动 Intent
startService(i);
}
});
btnStopService.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// 构建 Intent对象,指定关闭的对象为MyService服务
Intent i=new Intent(demo.this, MyService.class);
//以stopService 方法关闭 Intent
stopService(i);
}
});
}
}
package cn.iimob;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
/**
*
* @Project : servicedemo
* @Program Name : cn.iimob.MyService.java
* @Class Name : MyService
* @Description : 自定义 MyService 类继承 Service 类
* @Author : zh
* @Creation Date : 2011-11-3 上午09:49:00
* @ModificationHistory
* Who When What
* -------- ---------- -----------------------------------
* username 2011-11-3 TODO
*/
public class MyService extends Service {
/**
* 创建 Handler 对象,作为进程 传递 postDelayed 之用
*/
private Handler myhandler = new Handler();
/**
* 为了确认系统服务运行情况
*/
private int intCounter=0;
/**
* 成员变量 myTasks为Runnable对象,作为Timer之用
*/
private Runnable myTasks=new Runnable() {
/**
* 进程运行
*/
@Override
public void run() {
// TODO Auto-generated method stub
//递增counter整数,作为后台服务运行时间识别
intCounter++;
//以Log 对象在LogCat 里输出Log信息,监看服务运行情况
Log.i("Run Service", "Counter:"+Integer.toString(intCounter));
myhandler.postDelayed(myTasks, 1000);
}
};
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent,int startId){
myhandler.postDelayed(myTasks, 1000);
super.onStart(intent, startId);
Log.i("Start Service", "onStart");
}
@Override
public void onCreate(){
super.onCreate();
Log.i("Create Service", "onCreate");
}
@Override
public void onDestroy(){
//当服务结束,删除 mTasks 运行线程
myhandler.removeCallbacks(myTasks);
super.onDestroy();
Log.i("Destroy Service", "onDestroy");
}
}
<?xml version="1.0" encoding="utf-8"?>
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="开始Service" android:id="@+id/btnStartService" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="终止Service" android:id="@+id/btnStopService" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
package="cn.iimob"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".demo"
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:exported属情为true,表示此服务可被其他程序访问 -->
<service android:name=".MyService" android:exported="true" android:process=":remote"></service>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
转载于:https://www.cnblogs.com/skyblue/archive/2011/11/03/2234252.html
Android Service与Runnable整合并用相关推荐
- 浅谈android Service和BroadCastReceiver
1.题记 Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序. 广播接收者(BroadcastRece ...
- Android Service
Android Service 和BroadCast .Activity.以及ContentProvider并称为安卓四大组件.在日常开发中接触最多的是Activity,因为android其实就是一个 ...
- Android Service+Socket 联网交互
android中,联网操作有http连接和socket连接两大类.由于项目需要,我们采取的是Socket连接.鉴于平时连接频繁,因此把Socket连接放到Service里,需要从服务器端获取数据时,只 ...
- Android Service完全解析,关于服务你所需知道的一切(上)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的A ...
- android 浏览器源码分析,从源码出发深入理解 Android Service
原标题:从源码出发深入理解 Android Service 原文链接: 建议在浏览器上打开,删除了大量代码细节,:) 本文是 Android 系统学习系列文章中的第三章节的内容,介绍了 Android ...
- android service 学习(上)
转载自:http://www.cnblogs.com/allin/archive/2010/05/15/1736458.html Service是android 系统中的一种组件,它跟Activity ...
- android service 学习(下)
android service 学习(下) 通常每个应用程序都在它自己的进程内运行,但有时需要在进程间传递对象,你可以通过应用程序UI的方式写个运行在一个不同的进程中的service.在android ...
- android service是单例么,android 使用单例还是service?
stackover看到的回答,挺不错的. I'm quite new to Android development. When is it a good idea to create an Andro ...
- Android Service的思考(1)
在Android框架中,Service是比较难以理解的一部分,傻蛋查阅了相关资料和经过一系列的代码测试,准备写一个系列文章,尝试着把Service由浅入深的梳理一遍,帮助大家更快的掌握Android ...
最新文章
- 史上最大规模ACL大会放榜,百度10篇NLP论文被录用!
- MOS管驱动电路总结
- 微软技术专家为您解读深度学习
- P2084 进制转换
- flask学习笔记--蓝图
- (并查集)The Suspects
- 安装网站+服务器连接失败,为什么我都安装完成后是服务器连接失败啊
- nhibernate配置教程
- R语言实现地理探测器的流程及代码
- 山体滑坡动画用什么软件制作_做施工动画是用什么软件来做
- iTunes未能备份iPhone 多种详细解决方法
- 如何让paraview GUI软件启动时不弹出Welcome to paraview窗口
- 从数据备份恢复来看,iCloud和iTunes到底有什么区别?
- java soaoffice_SOAOFFICE是什么?
- 更新应用程序安卓apk时出现解析程序包时出现问题
- 字体粗细怎么设置 html,html中字体的粗细怎么设置?字体大小是font-size,那粗细怎么设置的?...
- 魅族16无信号服务器,魅族16信号差的解决办法
- acer蜂鸟swift1深度linux,最轻仅1.1kg Acer发布四款Swift蜂鸟笔电
- Word的常用操作和快捷键
- day 6 homework
热门文章
- what should you do if you want to have a high efficiency for communication
- what to look if you want to debug your docsify based website
- awesome xjtlu github project
- 为什么我的elec352稍微有点崩
- C++模板声明与实现分开--由此想到的编译,链接原理
- UNITY崩溃的日志
- Python数据类型(列表和元组)
- 2019年如何打造自己的“前端品牌”
- Shell脚本入门基础
- 8.Redis 数据备份与恢复