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 ...
随机推荐
- idl生成.h .c文件
1.从命令行执行 设置INCLUDE.LIB等,可先运行vsvars32.bat(C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\ ...
- Android带头像的用户注册页面
详细的图文可以到我的百度经验去查看:http://jingyan.baidu.com/article/cd4c2979eda109756e6e60de.html 首先是注册页面的布局: <?xm ...
- procedure的over(partition by ) function
转载自:http://www.2cto.com/database/201310/249722.html oracle的分析函数over(Partition by...) Sql代码 o ...
- ### 线性回归(Regression)
linear regression logistic regression softmax regression #@author: gr #@date: 2014-01-21 #@email: fo ...
- 【html】【12】特效篇--轮播图
必看参考: http://www.runoob.com/bootstrap/bootstrap-carousel-plugin.html 代码: <!DOCTYPE html> <h ...
- Android笔记之adb命令应用实例1(手机端与PC端socket通讯下)
通过adb和Android通讯需要引用adb相关的组件到项目中,分别为:adb.exe,AdbWinApi.dll,AdbWinUsbApi.dll. 可以在XXX\sdk\platform-tool ...
- Codevs 1001 舒适的路线 2006年 NOIP全国联赛提高组
1001 舒适的路线 2006年 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Z小镇是一个景色宜人的地方,吸引来自各地的观 ...
- CodeForces 538B
Description A number is called quasibinary if its decimal representation contains only digits 0 or 1 ...
- Wooden Sticks
Wooden Sticks Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(五)
这一节,用个简单的例子来调用下之前生成的service.ane 首先建一个flex手机项目 然后在构建路径中把ane引进来 可以看到此ane支持Android平台. serviceMobile.mxm ...