zoukankan      html  css  js  c++  java
  • Android入门:Service入门介绍

    一、Service介绍



    Service类似于Windows中的服务,没有界面,只是在后台运行;而服务不能自己运行,而是需要调用Context.startService(Intent intent);或Context.bindService(Intent intent)开启服务;


    服务分为两种:

    (1)访问者与服务无关,则关闭访问者之后,服务能够继续运行,使用startService();

    (2)访问者与服务有关(比如访问者需要调用服务提供的方法),则关闭访问者后,服务也要关闭,使用bindService();


    服务分为:

    (1)本地服务:访问者和服务在一个应用中;

    (2)远程服务:访问者和服务在不同应用者;


    注:如果我们想让服务开机自动运行,则可以创建一个广播接收者,并在onReceive中调用开启服务代码;


    我们这里只介绍“访问者与服务无关”的情况,以后再讲另一种情况;



    二、Service核心代码



    (1)创建继承Service的类

    (2)在AndroidManifest.xml中配置<service>

    (3)覆写Service中onCreate();方法


    开始服务:


    Intent service = new Intent(Context,XxxService.class);

    Context.startService(service);

    然后会调用onCreate()方法;


    停止服务:


    Intent service = new Intent(Context,XxxService.class);

    Context.stopService(service);

    然后会调用onDestroy()方法;


    三、Service生命周期


    1.通过Context.startService()启动服务


    onCreate()-->onStart()-->onDestroy()

    onCreate()在创建服务时调用,如果调用多次startService(),onCreate()方法仍然只被调用一次;

    onStart()在开始startService()调用时被调用,多次startService(),onStart()方法会被调用多次;

    onDestroy()在终止服务时调用;



    2.通过Context.bindService()启动服务


    onCreate()-->onBind()-->onUnbind()-->onDestroy()

    onBind()在绑定服务时调用,如果调用多次bindService(),则onBind()方法只被调用一次;

    onUnbind()在解除绑定(unBindService)时调用;


    四、MediaPlayer核心代码


    Mediaplayer player = MediaPlayer.create(getApplicationContext(), R.raw.xiazdong);//创建MediaPlayer对象

    player.setLooping(true);//循环播放

    player.start();  //开始播放

    player.stop();//停止播放

    player.release();//释放资源



    四、将MP3播放应用于Service


    此处只是实现点击Activity的播放音乐,则开始播放音乐,播放音乐的代码放到Service中,因此就算关闭应用,音乐也不会停止;

    效果如下:


    MainActivity中点击按钮后执行下面代码:

    private OnClickListener listener = new OnClickListener(){
    	@Override
    	public void onClick(View v) {
    		if(v==button){
    			Intent service = new Intent(MainActivity.this,MediaPlayerService.class);
    			MainActivity.this.startService(service);	//开启服务
    		}
    		if(v==button2){
    			Intent name = new Intent(MainActivity.this,MediaPlayerService.class);
    			MainActivity.this.stopService(name);		//停止服务
    		}
    	}
    };
    MediaPlayerService.java

    package com.xiazdong.mediaplayer;
    
    import android.app.Service;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.IBinder;
    
    public class MediaPlayerService extends Service {
    	private MediaPlayer player;
    	@Override
    	public IBinder onBind(Intent intent) {
    		return null;
    	}
    	@Override
    	public void onCreate() {
    		super.onCreate();
    		player = MediaPlayer.create(getApplicationContext(), R.raw.xiazdong);
    		player.setLooping(true);
    		try {
    			//因为MediaPlayer的create已经调用了prepare方法,因此此处直接start方法即可
    			player.start();  
    		} catch (Exception e) {
    			e.printStackTrace();
    		}  
    	}
    	@Override
    	public void onDestroy() {	//停止服务
    		super.onDestroy();
    		if(player!=null){
    			player.stop();
    			player.release();
    			player = null;
    		}
    	}
    }
    
    在AndroidManifest.xml中添加:

    <span style="font-size:12px;"><service android:name=".MediaPlayerService"/>  </span>




  • 相关阅读:
    爬虫笔记(四)------关于BeautifulSoup4解析器与编码
    sublime_text_2 ubuntu下无法输入中文 解决方法
    PHP 随手记
    PHP与apache环境配置
    5分钟学会如何创建spring boot项目
    Java 解压zip压缩包
    利用JavaScript来实现用动态检验密码强度
    金融行业是如何丢失1分钱的
    Java多线程的三种实现方式
    教你如何快速定制 SpringBoot banner
  • 原文地址:https://www.cnblogs.com/sowhat4999/p/4439869.html
Copyright © 2011-2022 走看看