Android学习之Service(1)--->Started方式



界面退出后进程程序还在运行,不会被杀死,如音乐播发器、后台下载等





注:本文只讨论Started方式











main.xml代码分析
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/btnStartService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Service"
/>
<Button
android:id="@+id/btnStopService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop Service"
/>
</LinearLayout>
创建一个class类ExampleService.java
package com.szy.service; import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log; public class ExampleService extends Service
{
private static final String TAG="ExampleService"; @Override
public void onCreate()
{
Log.i(TAG, "ExampleService-->onCreate");
super.onCreate();
} @Override
public void onStart(Intent intent, int startId)
{
// TODO Auto-generated method stub
super.onStart(intent, startId);
} @Override
public void onDestroy()
{
Log.i(TAG, "ExampleService-->onDestroy");
super.onDestroy();
} @Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i(TAG, "ExampleService-->onStartCommand");
return START_NOT_STICKY;
} @Override
public IBinder onBind(Intent intent)
{
return null;
} }
主对象类MainActivity.java
package com.szy.service; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity
{
private Button btnStartService;
private Button btnStopService;
@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(listener);
btnStopService.setOnClickListener(listener);
} private OnClickListener listener=new OnClickListener()
{ public void onClick(View v)
{
Intent intent=new Intent(MainActivity.this, ExampleService.class);
switch (v.getId())
{
case R.id.btnStartService:
startService(intent);
break;
case R.id.btnStopService:
stopService(intent);
break;
default:
break;
} }
};
}
在AndroidManifest.xml定义一下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.szy.service"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".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=".ExampleService" /> </application>
</manifest>
Android学习之Service(1)--->Started方式的更多相关文章
- 我有一壶酒 Android学习之Service(1)--->BinderService方式
本文只讨论扩展Binder类 创建一个Binder.xml <?xml version="1.0" encoding="utf-8"?> <L ...
- Android学习总结——Service组件
从Service的启动方式上,可以将Service分为Started Service和Bound Service.在使用Service时,要想系统能够找到此自定义Service,无论哪种类型,都需要在 ...
- 【Android学习】Service&Boradcast初步
Service初步 掌握Service概念 掌握Service分类 Service开发能力具备 了解Service和intentService类的区别 重点难点 StartService和BoundS ...
- Android 学习笔记 Service服务与远程通信...(AIDL)
PS:这一章节看的我有几分迷茫,不是很容易理解...不过还好总算是明白了一大半了...基本的迷惑是解决了... 学习内容: 1.跨应用启动服务... 2.跨应用绑定服务... 3.跨应用实现通信... ...
- Android 学习笔记 Service
PS:前几篇的内容光是上代码了,也没有细细的讲解..感觉这样写很不好..因此还是多一些讲解吧... 学习内容: 1.了解Service... 2.Service的启动与停止.. 3.绑定与取消绑定Se ...
- 【Android学习】四种布局方式
一.LinearLayout 线性布局,即一行展开或者一列展开,也可以嵌套,需要注意的属性如下: android:orentation //对齐方式 二.FrameLayout 帧布局,即一层层叠起 ...
- android学习笔记 Service
Service(服务): 长期后台运行的没有界面的组件 android应用什么地方需要用到服务? 天气预报:后台的连接服务器的逻辑,每隔一段时间获取最新的天气信息.股票显示:后台的连接服务器的逻辑,每 ...
- Android学习笔记④——页面的布局方式
FrameLayout(帧布局) 这个布局的特点是简单的默认把每一个视图组件都放在边框内且放在左上角,即使添加多个视图组件,他们也都是重叠在左上角,新的视图会遮挡住旧的视图.可以根据gravity来改 ...
- Android学习之Http使用Post方式进行数据提交(普通数据和Json数据)
转自:http://blog.csdn.net/wulianghuan/article/details/8626551 我们知道通过Get方式提交的数据是作为Url地址的一部分进行提交,而且对字节数的 ...
随机推荐
- Windows API 之 FormatMessage
FormatMessage Formats a message string. The function requires a message definition as input. The mes ...
- find文件后cp、rm
复制: find <src-path> -name 'some names' -exec cp {} <dest-path> \; find <src-path> ...
- 一个机器学习博客 ,包括 Standford公开课machine learning
http://blog.csdn.net/abcjennifer/article/category/1173803/4 http://blog.csdn.net/abcjennifer/article ...
- Primes on Interval
AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algori ...
- 认识cookie与session的区别与应用
通常我们所说的浏览器自动保存密码,下次不用登陆,网页换皮肤,用户引导,提示一次就不再出现的内容,大部分通过cookie或者session来实现的,在这次制作用户引导中,本人就用到了cookie的内容, ...
- C#输出日历
用C#输出日历,此功能可用于Ajax方式列出计划日程相关的内容,由于是C#控制输出,可以方便加上自己需要的业务处理逻辑. 1.控制台输出: using System; namespace 控制台日历 ...
- Vim常用命令【转载】
下面基本是vim的基本用法,刚开始学习可能有些不习惯.但贵在坚持,即使不习惯,也要坚持使用,做到不经过思考就能操作,你会发现真的很方便.很多操作可以通过不同的命令达到,我这里只列出常用的. 基础命令 ...
- textarea内容太多, 鼠标点击全部显示
strRight+="<td bordercolor='#DEDEDE' width='500px' height='50'><div title='"+data ...
- Nginx运行Laravel的配置
修改nginx.conf.修改前记得备份一下,万一改错了还能还原回去. server { listen 80; server_name localhost; set $root_path '/usr/ ...
- perl脚本之目录
来源: http://www.cnblogs.com/itech/archive/2013/02/20/2919204.html http://stackoverflow.com/questions/ ...