`
flycomos.lee
  • 浏览: 276328 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android service

阅读更多

Android 开发中,当需要创建在后台运行的程序的时候,就要使用到Service。Service 可以分为有无限生命和有限生命两种。

特 别需要注意的是Service跟Activities是不同的(简单来说可以理解为后台与前台的区别),例如,如果需要使用Service的话,需要调用 startService(),从而利用 startService()去调用Service中的OnCreate()和onStart()方法来启动一个后台的Service。

启动一个Service的过程如下:

context.startService()  ->onCreate()- >onStart()->Service running

其中onCreate()可以进行一些服务的初始化工作,onStart()则启动服务。

停止一个Service的过程如下:

context.stopService() | ->onDestroy() ->Service stop

接下来的实例是一个利用后台服务播放音乐的小例子,点击start运行服务,点击stop停止服务。

程序运行界面:

附件: 644c4660-e3a2-356f-baf6-5baeaf268c1d.png

代码:

定义服务,MyService.java

  1. package com.example;

  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.media.MediaPlayer;
  5. import android.os.IBinder;
  6. import android.util.Log;
  7. import android.widget.Toast;

  8. public class MyService extends Service {
  9.         private static final String TAG = "MyService";
  10.         MediaPlayer player;
  11.        
  12.         @Override
  13.         public IBinder onBind(Intent intent) {
  14.                 return null;
  15.         }
  16.        
  17.         @Override
  18.         public void onCreate() {
  19.                 Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
  20.                 Log.d(TAG, "onCreate");
  21.                
  22.                 player = MediaPlayer.create(this, R.raw.braincandy);//运行例子是,需要替换音乐的名称
  23.                 player.setLooping(false); // Set looping
  24.         }

  25.         @Override
  26.         public void onDestroy() {
  27.                 Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
  28.                 Log.d(TAG, "onDestroy");
  29.                 player.stop();
  30.         }
  31.        
  32.         @Override
  33.         public void onStart(Intent intent, int startid) {
  34.                 Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
  35.                 Log.d(TAG, "onStart");
  36.                 player.start();
  37.         }
  38. }
复制代码

除此之外还要在Manifest里面声明服务:

Xml代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.   package="com.example" android:versionCode="1" android:versionName="1.0">
  4.   <application android:icon="@drawable/icon" android:label="@string/app_name">
  5.     <activity android:name=".ServicesDemo" android:label="@string/app_name">
  6.       <intent-filter>
  7.         <action android:name="android.intent.action.MAIN" />
  8.         <category android:name="android.intent.category.LAUNCHER" />
  9.       </intent-filter>
  10.     </activity>
  11.     <service android:enabled="true" android:name=".MyService" />
  12.   </application>
  13.   <uses-sdk android:minSdkVersion="3" />
  14. </manifest>
复制代码

定义Activity,ServicesDemo.java

  1. package com.example;

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

  9. public class ServicesDemo extends Activity implements OnClickListener {
  10.   private static final String TAG = "ServicesDemo";
  11.   Button buttonStart, buttonStop;

  12.   @Override
  13.   public void onCreate(Bundle savedInstanceState) {
  14.     super.onCreate(savedInstanceState);
  15.     setContentView(R.layout.main);

  16.     buttonStart = (Button) findViewById(R.id.buttonStart);
  17.     buttonStop = (Button) findViewById(R.id.buttonStop);

  18.     buttonStart.setOnClickListener(this);
  19.     buttonStop.setOnClickListener(this);
  20.   }

  21.   public void onClick(View src) {
  22.     switch (src.getId()) {
  23.     case R.id.buttonStart:
  24.       Log.d(TAG, "onClick: starting srvice");
  25.       startService(new Intent(this, MyService.class));
  26.       break;
  27.     case R.id.buttonStop:
  28.       Log.d(TAG, "onClick: stopping srvice");
  29.       stopService(new Intent(this, MyService.class));
  30.       break;
  31.     }
  32.   }
  33. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics