实例示范如何使用 Android Services

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停止服务。

  代码:

  定义服务,MyService.java

  Java代码

  package com.example;

  import android.app.Service;

  import android.content.Intent;

  import android.media.MediaPlayer;

  import android.os.IBinder;

  import android.util.Log;

  import android.widget.Toast;

  public class MyService extends Service {

  private static final String TAG = “MyService”;

  MediaPlayer player;

  @Override

  public IBinder onBind(Intent intent) {

  return null;

  }

  @Override

  public void onCreate() {

  Toast.makeText(this, “My Service Created”, Toast.LENGTH_LONG).show();

  Log.d(TAG, “onCreate”);

  player = MediaPlayer.create(this, R.raw.braincandy);//运行例子是,需要替换音乐的名称

  player.setLooping(false); // Set looping

  }

  @Override

  public void onDestroy() {

  Toast.makeText(this, “My Service Stopped”, Toast.LENGTH_LONG).show();

  Log.d(TAG, “onDestroy”);

  player.stop();

  }

  @Override

  public void onStart(Intent intent, int startid) {

Toast.makeText(this, “My Service Started”, Toast.LENGTH_LONG).show();

  Log.d(TAG, “onStart”);

  player.start();

  }

  }

  package com.example;

  import android.app.Service;

  import android.content.Intent;

  import android.media.MediaPlayer;

  import android.os.IBinder;

  import android.util.Log;

  import android.widget.Toast;

  public class MyService extends Service {

  private static final String TAG = “MyService”;

  MediaPlayer player;

  @Override

  public IBinder onBind(Intent intent) {

  return null;

  }

  @Override

  public void onCreate() {

  Toast.makeText(this, “My Service Created”, Toast.LENGTH_LONG).show();

  Log.d(TAG, “onCreate”);

  player = MediaPlayer.create(this, R.raw.braincandy);//运行例子是,需要替换音乐的名称

  player.setLooping(false); // Set looping

  }

  @Override

  public void onDestroy() {

  Toast.makeText(this, “My Service Stopped”, Toast.LENGTH_LONG).show();

  Log.d(TAG, “onDestroy”);

  player.stop();

  }

  @Override

  public void onStart(Intent intent, int startid) {

  Toast.makeText(this, “My Service Started”, Toast.LENGTH_LONG).show();

  Log.d(TAG, “onStart”);

  player.start();

  }

  }

 

 

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

Xml代码
<?xml version=”1.0″ encoding=”utf-8″?> 
<manifest xmlns:android=”http://schemas.android.com/apk/res/android“ 
  package=”com.example” android:versionCode=”1″ android:versionName=”1.0″> 
  <application android:icon=”@drawable/icon” android:label=”@string/app_name”>

<activity android:name=”.ServicesDemo” android:label=”@string/app_name”> 
      <intent-filter> 
        <action android:name=”android.intent.action.MAIN” /> 
        <category android:name=”android.intent.category.LAUNCHER” /> 
      </intent-filter> 
    </activity> 
    <service android:enabled=”true” android:name=”.MyService” /> 
  </application> 
  <uses-sdk android:minSdkVersion=”3″ /> 
</manifest>  

 

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android
  package=”com.example” android:versionCode=”1″ android:versionName=”1.0″>
  <application android:icon=”@drawable/icon” android:label=”@string/app_name”>
    <activity android:name=”.ServicesDemo” android:label=”@string/app_name”>
      <intent-filter>
        <action android:name=”android.intent.action.MAIN” />
        <category android:name=”android.intent.category.LAUNCHER” />
      </intent-filter>
    </activity>
    <service android:enabled=”true” android:name=”.MyService” />
  </application>
  <uses-sdk android:minSdkVersion=”3″ />
</manifest>

 

 

  定义Activity,ServicesDemo.java

  Java代码

  package com.example;

  import android.app.Activity;

import android.content.Intent;

  import android.os.Bundle;

  import android.util.Log;

  import android.view.View;

  import android.view.View.OnClickListener;

  import android.widget.Button;

  public class ServicesDemo extends Activity implements OnClickListener {

  private static final String TAG = “ServicesDemo”;

  Button buttonStart, buttonStop;

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  buttonStart = (Button) findViewById(R.id.buttonStart);

  buttonStop = (Button) findViewById(R.id.buttonStop);

  buttonStart.setOnClickListener(this);

  buttonStop.setOnClickListener(this);

  }

  public void onClick(View src) {

  switch (src.getId()) {

  case R.id.buttonStart:

  Log.d(TAG, “onClick: starting srvice”);

  startService(new Intent(this, MyService.class));

  break;

  case R.id.buttonStop:

  Log.d(TAG, “onClick: stopping srvice”);

  stopService(new Intent(this, MyService.class));

  break;

  }

  }

  }

 

 

  package com.example;

  import android.app.Activity;

  import android.content.Intent;

  import android.os.Bundle;

  import android.util.Log;

  import android.view.View;

  import android.view.View.OnClickListener;

  import android.widget.Button;

  public class ServicesDemo extends Activity implements OnClickListener {

  private static final String TAG = “ServicesDemo”;

  Button buttonStart, buttonStop;

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

buttonStart = (Button) findViewById(R.id.buttonStart);

  buttonStop = (Button) findViewById(R.id.buttonStop);

  buttonStart.setOnClickListener(this);

  buttonStop.setOnClickListener(this);

  }

  public void onClick(View src) {

  switch (src.getId()) {

  case R.id.buttonStart:

  Log.d(TAG, “onClick: starting srvice”);

  startService(new Intent(this, MyService.class));

  break;

  case R.id.buttonStop:

  Log.d(TAG, “onClick: stopping srvice”);

  stopService(new Intent(this, MyService.class));

  break;

  }

  }

  }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值