ANDROID_MARS学习笔记_S01原始版_016_Service
一、代码
1.xml
(1)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/startService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="StartService"
/>
<Button
android:id="@+id/stopService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="StopService"
/>
</LinearLayout>
(2)AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.service"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".TestActivity"
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=".FirstService"/>
</application> </manifest>
2.java
(1)TestActivity.java
package com.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 TestActivity extends Activity {
/** Called when the activity is first created. */
private Button startServiceButton = null;
private Button stopServiceButton = null; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startServiceButton = (Button) findViewById(R.id.startService);
startServiceButton.setOnClickListener(new StartServiceListener());
stopServiceButton = (Button) findViewById(R.id.stopService);
stopServiceButton.setOnClickListener(new StopServiceListener());
System.out.println("Activity onCreate");
} class StartServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
startService(intent);
}
} class StopServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
stopService(intent);
}
}
}
(2)FirstService.java
package com.service; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class FirstService extends Service { @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("Service onBind");
return null;
} //当创建一个Servcie对象之后,会首先调用这个函数
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("Service onCreate");
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("flags--->" + flags);
System.out.println("startId--->" + startId);
System.out.println("Service onStartCommand");
return START_NOT_STICKY;
} @Override
public void onDestroy() {
// TODO Auto-generated method stubo
System.out.println("Service onDestory");
super.onDestroy();
}
}
ANDROID_MARS学习笔记_S01原始版_016_Service的更多相关文章
- ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast
一.代码 1.xml(1)radio.xml <?xml version="1.0" encoding="utf-8"?> <LinearLa ...
- ANDROID_MARS学习笔记_S01原始版_004_TableLayout
1.xml <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android ...
- ANDROID_MARS学习笔记_S01原始版_003_对话框
1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest ...
- ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用
一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...
- ANDROID_MARS学习笔记_S01原始版_001_Intent
一.Intent简介 二.代码 1.activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.co ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER005_用广播BroacastReciever实现后台播放不更新歌词
一.代码流程1.自定义一个AppConstant.LRC_MESSAGE_ACTION字符串表示广播"更新歌词" 2.在PlayerActivity的onResume()注册Bro ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词
一.流程分析 1.点击播放按钮,会根据lrc名调用LrcProcessor的process()分析歌词文件,得到时间队列和歌词队列 2.new一个hander,把时间队列和歌词队列传给自定义的线程类U ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3
一.简介 1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity 2.PlayerActivity通过 ...
- ANDROID_MARS学习笔记_S01原始版_022_MP3PLAYER002_本地及remote标签
一.简介 1.在main.xml中用TabHost.TabWidget.FrameLayout标签作布局 2.在MainActivity中生成TabHost.TabSpec,调用setIndicato ...
随机推荐
- vc静态加载dll和动态加载dll
如果你有a.dll和a.lib,两个文件都有的话可以用静态加载的方式: message函数的声明你应该知道吧,把它的声明和下面的语句写到一个头文件中 #pragma comment(lib, &quo ...
- 怎么关闭InstantRun
Settings → Build, Execution, Deployment → Instant Run and uncheck Enable Instant Run.
- Hibernate+struts+JqueryAjax+jSON实现无刷新三级联动
看网上JqueryAjax三级联动的例子讲不是很全,代码也给的不是很全,给初学者带来一定的难度.小弟自己写了一个,可能有些地方不是很好,希望大家能够提出建议. 用的是Hibernate+struts2 ...
- Java JDK1.5、1.6、1.7新特性整理(转)
原文链接:http://www.cnblogs.com/tony-yang-flutter/p/3503935.html 一.Java JDK1.5的新特性 1.泛型: List<String& ...
- 常用的 Internet Browser adds-on/浏览器插件
主要应用在Firefox, 或 Google Chrome 一.AdBlockPlus 广告屏蔽软件 二.GreaseMonkey 多样化网页 三.Dictionary.com 弹出单词的解释,来自 ...
- mybatis学习(一)一个在idea下的实例
今天总结的是mybatis,首先说mybatis是什么? MyBatis 是一个简化和实现了 Java 数据持久化层(persistence layer)的开源框架,它抽象了大量的 JDBC 冗余代 ...
- 伪分布式环境下命令行正确运行hadoop示例wordcount
首先确保hadoop已经正确安装.配置以及运行. 1. 首先将wordcount源代码从hadoop目录中拷贝出来. [root@cluster2 logs]# cp /usr/local/h ...
- Google设计理念
Google的十大信条 我们首次拟就这“十大信条”还是在Google刚刚成立没几年的时候.此后,我们时常重新审视这份清单,看看它是否依然适用.我们希望这些信条永不过时,而您也可以监督我们是否遵守了这些 ...
- LPC17XX 数据手册摘要之系统时钟与功率控制
系统时钟与功率控制 一.系统时钟 LPC17XX有三个独立的时钟振荡器,分别是主振荡器(MIAN_OSC).内部RC振荡器(IRC_OSC).实时时钟振荡器(RTC_OSC).LPC17XX时钟框图如 ...
- OpenSessionInViewFilter与org.springframework.dao.InvalidDataAccessApiUsageException
报错:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in r ...