package com.example.myapp4;

 import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
/**
* Android 记录Service的生命周期(配置文件中需要引入Service类名)
* @author shaobn
*
*/
public class MainActivity extends ActionBarActivity {
private Button startButton;
private Button stopButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) this.findViewById(R.id.button1);
stopButton = (Button) this.findViewById(R.id.button2);
startButton.setOnClickListener(new MyClick());
stopButton.setOnClickListener(new MyClick());
}
public class MyClick implements View.OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button1:
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
break; case R.id.button2:
Intent intent2 = new Intent(MainActivity.this,MyService.class);
stopService(intent2);
break;
}
} }
}
 package com.example.myapp4;

 import android.app.Service;
import android.content.Intent;
import android.os.IBinder; public class MyService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("--create");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("--onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("--onDestroy");
super.onDestroy();
} }

Android 测试Service的生命周期的更多相关文章

  1. android基础---->service的生命周期

    服务是一个应用程序组件代表应用程序执行一个长时间操作的行为,虽然不与用户交互或供应功能供其它应用程序使用.它和其他的应用对象一样,在他的宿主进程的主线程中运行.今天我们开始android中普通serv ...

  2. Android中service的生命周期

    Service作为Android四大组件 Service Activity ContentProvider BroadcastReceiver 之一,应用非常广泛,和Activity一样,Servic ...

  3. Android activity和service的生命周期对比

    1Activity生命周期 七个方法 1. void onCreate(Bundle savedInstanceState) 当Activity被第首次加载时执行.我们新启动一个程序的时候其主窗体的o ...

  4. Android Service的生命周期

    service的生命周期,从它被创建开始,到它被销毁为止,可以有两条不同的路径: A started service 被开启的service通过其他组件调用 startService()被创建. 这种 ...

  5. android学习-Activity和Service的生命周期

    详细请跳转原网页Activity和Service的生命周期(图) 不解释,不懂算我输 Activity的生命周期(图) Service的声明周期

  6. Android菜鸟的成长笔记(19)——Service的生命周期

    前面两篇文章介绍了关于Service的两种启动方式,简要总结如下: Context.startService() Context.bindService() 1. startService()的目的是 ...

  7. 18_Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用,综合服务案例,编写一个应用程序调用远程支付宝远程服务场景

    ============================================================================ 服务的生命周期: 一.采用start的方式开始 ...

  8. 8.1.1 Service的生命周期

    2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Android服务,本章主要介绍了Android系统 中的服 ...

  9. Activity和Service的生命周期(图)

    1.Activity的生命周期 情形一.一个单独的Activity的正常的生命过程是这样的:onCreate->onStart->onPause->onStop->onDest ...

随机推荐

  1. WCF TCP 错误代码 10061: 由于目标计算机积极拒绝

    表象是连不上服务端,本质原因多种多样,网络硬件问题导致的网络不通.服务本身问题或没有启动.或者防火墙阻隔等等不一而足. 1.ping看服务端能否ping通: 2.telnet ip地址 端口 ,看看是 ...

  2. JQuery通过$(xxx...)返回对象

    var JQ = function () { return new JQ.prototype.init(); }; JQ.prototype.init = function () { }; JQ.pr ...

  3. <q>标签,短文本引用;<blockquote>标签,长文本引用

    <q>标签,短文本引用 <q>引用文本</q>,默认显示双引号,不需要在文本中添加 <blockquote>标签,长文本引用 浏览器对<block ...

  4. [LeetCode]题解(python):052-N-Queens II

    题目来源 https://leetcode.com/problems/n-queens-ii/ Follow up for N-Queens problem. Now, instead outputt ...

  5. 【C++】error C4146: 一元负运算符应用于无符号类型,结果仍为无符号类型

    刷leetcode 263.uglynumber时,代码如下: class Solution { public: bool isUgly(int num) { int temp = num; ) re ...

  6. Dictionary、SortedDictionary、Hashtable 、SortedList

    HashTable数据结构存在问题:空间利用率偏低.受填充因子影响大.扩容时所有的数据需要重新进行散列计算.虽然Hash具有O(1)的数据 检索效率,但它空间开销却通常很大,是以空间换取时间.所以Ha ...

  7. logstash

    logstash作为数据搜集器,主要分为三个部分:input->filter->output  作为pipeline的形式进行处理,支持复杂的操作,如发邮件等 input配置数据的输入和简 ...

  8. LeetCode Game of Life

    原题链接在这里:https://leetcode.com/problems/game-of-life/ 题目: According to the Wikipedia's article: " ...

  9. Nested transactions in stored procedure of SQLServer

    question: if the nested transaction encountered an exception, then rollbacked. How about the outer t ...

  10. loop_nslookup

    function loop_nslookup() { $baseFolder = "D:\ps_toolkit" $ip_list = "$baseFolder\ip_l ...