Chinaunix首页 | 论坛 | 博客
  • 博客访问: 199422
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2014-04-10 07:58:14

01_25_Service初步(一)



点击(此处)折叠或打开

  1. //TestActivity.java
  2. package mars.service1;

  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;

  9. public class TestActivity extends Activity {
  10.     /** Called when the activity is first created. */
  11.     private Button startServiceButton = null;
  12.     private Button stopServiceButton = null;

  13.     @Override
  14.     public void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.main);
  17.         startServiceButton = (Button) findViewById(R.id.startService);
  18.         startServiceButton.setOnClickListener(new StartServiceListener());
  19.         stopServiceButton = (Button) findViewById(R.id.stopService);
  20.         stopServiceButton.setOnClickListener(new StopServiceListener());
  21.         System.out.println("Activity onCreate");
  22.     }

  23.     class StartServiceListener implements OnClickListener {
  24.         @Override
  25.         public void onClick(View v) {
  26.             Intent intent = new Intent();
  27.             intent.setClass(TestActivity.this, FirstService.class);
  28.             startService(intent);
  29.         }
  30.     }

  31.     class StopServiceListener implements OnClickListener {
  32.         @Override
  33.         public void onClick(View v) {
  34.             Intent intent = new Intent();
  35.             intent.setClass(TestActivity.this, FirstService.class);
  36.             stopService(intent);
  37.         }
  38.     }
  39. }


点击(此处)折叠或打开

  1. //FirstService.java
  2. package mars.service1;

  3. import android.app.Service;
  4. import android.content.Intent;
  5. import android.os.Binder;
  6. import android.os.IBinder;

  7. public class FirstService extends Service {
  8.     
  9.     @Override
  10.     public IBinder onBind(Intent intent) {
  11.         System.out.println("Service onBind");
  12.         return null;
  13.     }

  14.     //当创建一个Servcie对象之后,会首先调用这个函数
  15.     @Override
  16.     public void onCreate() {
  17.         // TODO Auto-generated method stub
  18.         super.onCreate();
  19.         System.out.println("Service onCreate");
  20.     }

  21.     /*每次启动Service时都调用onStartCommand()方法*/
  22.     @Override
  23.     public int onStartCommand(Intent intent, int flags, int startId) {
  24.         // TODO Auto-generated method stub
  25.         System.out.println("flags--->" + flags);
  26.         System.out.println("startId--->" + startId);
  27.         System.out.println("Service onStartCommand");
  28.         return START_NOT_STICKY;
  29.     }

  30.     /*onDestroy()添加释放资源*/
  31.     @Override
  32.     public void onDestroy() {
  33.         // TODO Auto-generated method stubo
  34.         System.out.println("Service onDestory");
  35.         super.onDestroy();
  36.     }
  37. }

点击StartService按钮->StartService按钮->StopService->StartService按钮->StopService按钮,结果如下
04-10 00:02:34.381: I/System.out(473): Service onCreate
04-10 00:02:34.381: I/System.out(473): flags--->2
04-10 00:02:34.381: I/System.out(473): startId--->1
04-10 00:02:34.381: I/System.out(473): Service onStartCommand
04-10 00:02:40.952: I/System.out(473): flags--->2
04-10 00:02:40.952: I/System.out(473): startId--->2
04-10 00:02:40.952: I/System.out(473): Service onStartCommand
04-10 00:02:45.381: I/System.out(473): Service onDestory
04-10 00:02:46.611: I/System.out(473): Service onCreate
04-10 00:02:46.611: I/System.out(473): flags--->2
04-10 00:02:46.611: I/System.out(473): startId--->1
04-10 00:02:46.611: I/System.out(473): Service onStartCommand
04-10 00:02:48.851: I/System.out(473): Service onDestory




阅读(220) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~