码迷,mamicode.com
首页 > 移动开发 > 详细

Android 测试Service的生命周期

时间:2015-09-14 23:50:54      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

 1 package com.example.myapp4;
 2 
 3 import android.support.v7.app.ActionBarActivity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.Menu;
 7 import android.view.MenuItem;
 8 import android.view.View;
 9 import android.widget.Button;
10 /**
11  * Android 记录Service的生命周期(配置文件中需要引入Service类名)
12  * @author shaobn
13  *
14  */
15 public class MainActivity extends ActionBarActivity {
16     private Button startButton;
17     private Button stopButton;
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         startButton = (Button) this.findViewById(R.id.button1);
23         stopButton = (Button) this.findViewById(R.id.button2);
24         startButton.setOnClickListener(new MyClick());
25         stopButton.setOnClickListener(new MyClick());
26     }
27     public class MyClick implements View.OnClickListener{
28         @Override
29         public void onClick(View arg0) {
30             // TODO Auto-generated method stub
31             switch (arg0.getId()) {
32             case R.id.button1:
33                 Intent intent = new Intent(MainActivity.this, MyService.class);
34                 startService(intent);
35                 break;
36 
37             case R.id.button2:
38                 Intent intent2 = new Intent(MainActivity.this,MyService.class);
39                 stopService(intent2);
40                 break;
41             }
42         }
43         
44     }
45 }
 1 package com.example.myapp4;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.IBinder;
 6 
 7 public class MyService extends Service {
 8     @Override
 9     public void onCreate() {
10         // TODO Auto-generated method stub
11         System.out.println("--create");
12         super.onCreate();
13     }
14     @Override
15     public int onStartCommand(Intent intent, int flags, int startId) {
16         // TODO Auto-generated method stub
17         System.out.println("--onStartCommand");
18         return super.onStartCommand(intent, flags, startId);
19      } 
20     @Override
21     public IBinder onBind(Intent arg0) {
22         // TODO Auto-generated method stub
23         return null;
24     }
25     @Override
26     public void onDestroy() {
27         // TODO Auto-generated method stub
28         System.out.println("--onDestroy");
29         super.onDestroy();
30     }
31 
32 }

 

Android 测试Service的生命周期

标签:

原文地址:http://www.cnblogs.com/assassin666/p/4808534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!