赵雅智:service_startService生命周期
案例演示
布局文件
<RelativeLayout 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"
tools:context="${packageName}.${activityClass}" > <Button
android:id="@+id/btn_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="context.startService" /> <Button
android:id="@+id/btn_stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_start"
android:onClick="click"
android:text="context.strpService" /> </RelativeLayout>
注冊service
<? xml version="1.0" encoding="utf-8"? >
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_servicedemo"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.android_servicedemo.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="ServiceTese"
android:permission="com.example.permission.servers" >
<intent-filter>
<action android:name="com.example.serversdemo.action" />
</intent-filter>
</service>
</application> </manifest>
主activity
package com.example.android_servicedemo; 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; public class MainActivity extends Activity {
private Intent service;
MyConn myConn;
public static final String TAG = "MainActivity"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 隐士意图的启动
service = new Intent();
service.setAction("com.example.serversdemo.action");
} public void click(View v) {
int id = v.getId();
switch (id) {
/*
* 启动服务
*/
case R.id.btn_start:
this.startService(service);
break;
/*
* 停止服务
*/
case R.id.btn_stop:
this.stopService(service);
break;
default:
break;
}
} }
执行结果
生命周期为
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhpMjEyOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
onbindService见http://blog.csdn.net/zhaoyazhi2129/article/details/32712073
赵雅智:service_startService生命周期的更多相关文章
- 赵雅智_Fragment生命周期
官网帮助文档链接: http://developer.android.com/guide/components/fragments.html 主要看两张图.和跑代码 一,Fragment的生命周 w ...
- 赵雅智_service生命周期
Android中的服务和windows中的服务是类似的东西,服务一般没实用户操作界面.它执行于系统中不easy被用户发觉,能够使用它开发如监控之类的程序. 服务的开发步骤 第一步:继承Service类 ...
- 赵雅智:android教学大纲
带下划线为详细内容链接地址.点击后可跳转.希望给大家尽一些微薄之力.眼下还在整理中 教学章节 教学内容 学时安排 备注 1 Android高速入门 2 Android模拟器与常见命令 3 Androi ...
- 赵雅智:service_bindService生命周期
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhpMjEyOQ==/font/5a6L5L2T/fontsize/400/fill/I0 ...
- 赵雅智_BroadcastReceiver
BroadcastReceiver 用于接收程序(包含用户开放的程序和系统内建程序)所发出的Broadcast intent 耗电量 开机启动 窃取别人短信 窃取别人电话 开发: 创建须要启动的Br ...
- 赵雅智_Swift(1)_swift简单介绍及类型
Swift 是 iOS 和 OS X 应用开发的一门新语言. 假设你有 C 或者 Objective-C 开发经验, Swift 的非常多内容都是你熟悉的. Swift 的类型是在 C 和 Objec ...
- 赵雅智_Fragment
当我们须要动态的多界面切换的时候,就须要将UI元素和Activity融合成一个模块. 在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单个界面动态改变.在4.0或以上系统中就能 ...
- 赵雅智_ContentProvider
ContentProvider介绍 ContentProvider是不同应用程序之间进行交换数据的标志API 也就是说:一个应用程序通过ContentProvider暴露自己的数据操作接口,那么无论该 ...
- 赵雅智:js知识点汇总
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhpMjEyOQ==/font/5a6L5L2T/fontsize/400/fill/I0 ...
随机推荐
- GUI练习——列出指定目录内容
需求: 一个窗体里.在文本框输入路径后,摁回车键或者点击"转到"按钮后: 若路径合法,程序会自动在文本域里显示该路径下的文件目录:若路径非法,则弹出对话框,告之你路径非法.点击&q ...
- 关于express4不再支持body-parser
express的bodyParser能将表单里的数据格式化,bodyParser原是绑定在express中的,但从express4开始,不在绑定了 如果依然直接使用app.use(express.bo ...
- eclipse注解快捷键
Search 功能:全局文件内容搜索快捷键: Ctrl + H -------------------------------------------------------------------- ...
- [转载]Heritrix 提高效率的若干方法
摘自http://blog.sina.com.cn/s/blog_6cc084c90100nf39.html --------------------------------------------- ...
- hdu 3518 Boring counting 后缀数组
题目链接 根据height数组的性质分组计算. #include <iostream> #include <vector> #include <cstdio> #i ...
- hdu 3572 Escape 网络流
题目链接 给一个n*m的图, 里面有一些点, '.'代表空地, '#'代表墙, 不可以走, '@'代表大门, 可以有多个, 'X'代表人, 问所有人都走出大门需要的最短时间, 每一时刻一个格子只能有一 ...
- ortoiseSVN无法编辑日志信息的解决方法
提交时忘记编写日志,想重新编辑日志信息,弹出错误提示: DAV 请求失败:可能是版本库的 pre-revprop-change 钩子执行失败或者不存在 至少有一个属性变更失败:版本库未改变 设置属性 ...
- 记录.net 中的常见术语
--Entity Framework和NHibernate --EF和NH都是一种ORM技术.就是对象关系模型映射. --NHibernate和Entity Framework 4.0优劣势争论 -- ...
- python基础学习笔记1
一.字符串: 1.不可变性.分片赋值对于字符串是不合法的. 2.字符串格式化 % eg: print 'The price is: %d' % 30 print 'The price is: %.2f ...
- 侯捷C++ Type traits(类型萃取
泛型編程編出來的代碼,適用於任何「吻合某種條件限制」的資料型別.這已成為撰寫可復用代碼時的一個重要選擇.然而,總有一些時候,泛型不夠好 — 有時候是因為不同的型別差距過大,難以產生一致的泛化實作版本. ...