ANDROID_MARS学习笔记_S01原始版_016_Service

一、代码
1.xml
(1)main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7 <Button
 8     android:id="@+id/startService"
 9     android:layout_width="fill_parent"
10     android:layout_height="wrap_content"
11     android:text="StartService"
12     />
13 <Button
14     android:id="@+id/stopService"
15     android:layout_width="fill_parent"
16     android:layout_height="wrap_content"
17     android:text="StopService"
18     />
19 </LinearLayout>

(2)AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.service"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="21" />
10
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name=".TestActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         <service android:name=".FirstService"/>
26     </application>
27
28 </manifest>

2.java
(1)TestActivity.java

 1 package com.service;
 2
 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
10 public class TestActivity extends Activity {
11     /** Called when the activity is first created. */
12     private Button startServiceButton = null;
13     private Button stopServiceButton = null;
14
15     @Override
16     public void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.main);
19         startServiceButton = (Button) findViewById(R.id.startService);
20         startServiceButton.setOnClickListener(new StartServiceListener());
21         stopServiceButton = (Button) findViewById(R.id.stopService);
22         stopServiceButton.setOnClickListener(new StopServiceListener());
23         System.out.println("Activity onCreate");
24     }
25
26     class StartServiceListener implements OnClickListener {
27         @Override
28         public void onClick(View v) {
29             Intent intent = new Intent();
30             intent.setClass(TestActivity.this, FirstService.class);
31             startService(intent);
32         }
33     }
34
35     class StopServiceListener implements OnClickListener {
36         @Override
37         public void onClick(View v) {
38             Intent intent = new Intent();
39             intent.setClass(TestActivity.this, FirstService.class);
40             stopService(intent);
41         }
42     }
43 }

(2)FirstService.java

 1 package com.service;
 2
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.Binder;
 6 import android.os.IBinder;
 7
 8 public class FirstService extends Service {
 9
10     @Override
11     public IBinder onBind(Intent intent) {
12         // TODO Auto-generated method stub
13         System.out.println("Service onBind");
14         return null;
15     }
16
17     //当创建一个Servcie对象之后,会首先调用这个函数
18     @Override
19     public void onCreate() {
20         // TODO Auto-generated method stub
21         super.onCreate();
22         System.out.println("Service onCreate");
23     }
24
25     @Override
26     public int onStartCommand(Intent intent, int flags, int startId) {
27         // TODO Auto-generated method stub
28         System.out.println("flags--->" + flags);
29         System.out.println("startId--->" + startId);
30         System.out.println("Service onStartCommand");
31         return START_NOT_STICKY;
32     }
33
34     @Override
35     public void onDestroy() {
36         // TODO Auto-generated method stubo
37         System.out.println("Service onDestory");
38         super.onDestroy();
39     }
40 }
时间: 2024-08-05 04:51:28

ANDROID_MARS学习笔记_S01原始版_016_Service的相关文章

ANDROID_MARS学习笔记_S01原始版_013_广播机制二

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S01原始版_012_广播机制一

一.简介 二.代码1.xml(1)activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width

ANDROID_MARS学习笔记_S01原始版_007_Handler及线程的简单使用

一.运行结果 一.代码1.xml(1)activity_main.xml 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_heig

ANDROID_MARS学习笔记_S01原始版_008_Handler(异步消息处理机制)

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3

一.简介 1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity 2.PlayerActivity通过android.media.MediaPlayer实现播放,暂停.停止 二.代码1.xml(1)player.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:andr

ANDROID_MARS学习笔记_S01原始版_010_ContentProvider

一.简介 一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="fill

ANDROID_MARS学习笔记_S01原始版_005_ProgressBar

一.代码 1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_p

ANDROID_MARS学习笔记_S01原始版_006_ListView

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="fill_pare

ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast

一.代码 1.xml(1)radio.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_