一、代码
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();
}
}

最新文章

  1. 原生JS实现全屏切换以及导航栏滑动隐藏及显示——重构前
  2. android键盘输入读取
  3. echarts图表第一个案例
  4. LeetCode----Array
  5. 自动化测试管理平台ATMS(V2.0.2_8.19)下载
  6. [翻译] 使用ASP.NET MVC操作过滤器记录日志
  7. 获取一个 app 的 URL Scheme 的方法:
  8. Hadoop学习9--动态增加datanode
  9. DOM基础总结
  10. Navigation Drawer(导航抽屉)
  11. php随笔1-php图片处理
  12. [LeetCode]题解(python):129-Sum Root to Leaf Numbers
  13. 最新百度地图支持Fragment(注意事项)(转)
  14. 3)Javascript设计模式:Observer模式
  15. PKI信息安全知识点详细解答包含HTTPS
  16. 三数之和的golang实现
  17. spring官网上下载历史版本的spring插件,springsource-tool-suite
  18. 也说性能测试,顺便说python的多进程+多线程、协程
  19. [CNN] Tool - Deep Visualization
  20. confirm提示弹出确定和取消按钮

热门文章

  1. JS 日期工具类-基于yDate
  2. WIN8+VS2013编写发布WCF之一(编写)
  3. 解决:The Operation couldn&#39;t be completed.(LaunchServicesError error 0.)
  4. WPF的TextBox的焦点获取与失去焦点的死循环解决方案
  5. Spring集成PageHelper的简单用法
  6. [PR &amp; ML 5] [Introduction] Decision Theory
  7. springmvc(六)——视图和视图解析器
  8. File Operation using SHFileOperation
  9. asp.net弹出框后页面走样
  10. NodeJS较高版本对connect支持的问题