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地址的一部分进行提交,而且对字节数的 ...
随机推荐
- 在选定的数据源上未找到名为“TitleSub”的字段或属
在.NET开发过程中时常会遇到“在选定的数据源上未找到名为“TitleSub”的字段或属性”的错误”,导致这类错误的原因有很多,在我的项目中,详细情况是这样:1.有两个控件:DropDownList类 ...
- Eclipse配置--智能补全
定位到:Windows→Preferences→Java→Editor→Content Assist 将Auto Activation triggers for java的默认值“.”修改为" ...
- UTF8,UTF16,UTF32,UTF16-LE,UTF16-BE,GBK 之间的转换
Unicode是Unicode.org制定的编码标准,目前得到了绝大部分操作系统和编程语言的支持.Unicode.org官方对Unicode的定义是:Unicode provides a unique ...
- APUE读书笔记:关于sigsuspend
sigsuspend是一个原子操作,为了防止信号丢失而存在的,具体含义看下函数原型. int sigsuspend(const sigset_t *mask); 先忽略参数,sigsuspend完成的 ...
- 根据View获取该控制器
//根据View获取控制器 - (UIViewController*)viewController { for (UIView* next = [self superview]; next; next ...
- JavaScript CSS Style属性对照表
JavaScript CSS Style属性对照表 盒子标签和属性对照 CSS语法 (不区分大小写) JavaScript语法 (区分大小写) border border border-bottom ...
- libprotobuff8.so not found
http://stackoverflow.com/questions/25518701/protobuf-cannot-find-shared-libraries
- IOS 中常用站位符
CGPoint.CGRect等可以转化为字符串打印出来 如: NSLog(@"-------------%@",NSStringFromCGPoint(point)); ...
- WPF之让ListView中的CheckBox居中显示
第一步:在资源中定义一个居中的样式: <Window.Resources> <Style x:Key="ListViewItemStyle" TargetType ...
- wpf CollectionViewSource的运用
实体类: 员工类: public class Department : ObservableCollection<Employee> { public string DepName { g ...