ZNDS智能电视网 推荐当贝市场

TV应用下载 / 资源分享区

软件下载 | 游戏 | 讨论 | 电视计算器

综合交流 / 评测 / 活动区

交流区 | 测硬件 | 网站活动 | Z币中心

新手入门 / 进阶 / 社区互助

新手 | 你问我答 | 免费刷机救砖 | ROM固件

查看: 15289|回复: 0
上一主题 下一主题
[教程]

第十四讲:Service入门指南

[复制链接]
跳转到指定楼层
楼主
发表于 2013-8-28 16:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
680本讲内容:Service   
1、Service的概念   
2、Service的生命周期   
3、实例:控制音乐播放的Service
本讲源代码:
  Service对象不能自己启动,需要通过某个Activity、Service或者其他Context对象来启动。启动的方法有两种,Context.startService和Context.bindService()。两种方式的生命周期是不同的,具体如下所示。Context.startService方式的生命周期:   
启动时,startService –> onCreate() –> onStart()   
停止时,stopService –> onDestroy()Context.bindService方式的生命周期:   
绑定时,bindService  -> onCreate() –> onBind()   
解绑定时,unbindService –>onUnbind() –> onDestory()下面我们用一个可以控制在后台播放音乐的例子来演示刚才所学知识,同学们可以通过该例子可以明显看到通过绑定方式运行的Service在绑定对象被销毁后也被销毁了。
  下面把代码分享如下:1、建立一个新项目名字叫 Lesson14_HelloService,Activity起名叫MainHelloService.java2、res/layout/main.xml中代码写成   
  1. < ?xml version="1.0" encoding="utf-8"?>   
    <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">   
    <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="音乐播放服务" android:textsize="25sp" android:layout_margintop="10dp">   
    <button android:text="开启音乐播放服务" android:textsize="20sp" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp">   
    </button>   
    <button android:text="停止音乐播放服务" android:textsize="20sp" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp">   
    </button>   
       
    <button android:text="绑定音乐播放服务" android:textsize="20sp" android:id="@+id/Button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp">   
    </button>   
    <button android:text="解绑定音乐播放服务" android:textsize="20sp" android:id="@+id/Button04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp">   
    </button>   
    </textview></linearlayout>   
复制代码
2、在res目录中建立一个raw目录,并把一个音乐文件babayetu.mp3拷贝进来3、在Activity的同目录新建一个service文件MusicService.java   
  1. package android.basic.lesson14;   
       
    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 MusicService extends Service {   
       
            //为日志工具设置标签   
            String tag ="MusicService";           
       
            //定义音乐播放器变量   
            MediaPlayer mPlayer;   
       
            //其他对象通过bindService方法通知该Service时该方法会被调用   
            @Override   
            public IBinder onBind(Intent intent) {   
                    Toast.makeText(this,"MusicService onBind()",Toast.LENGTH_SHORT).show();   
                    Log.i(tag, "MusicService onBind()");   
                    mPlayer.start();   
                    return null;   
            }   
       
            //其他对象通过unbindService方法通知该Service时该方法会被调用   
            @Override   
            public boolean onUnbind(Intent intent){   
                    Toast.makeText(this, "MusicService onUnbind()", Toast.LENGTH_SHORT).show();   
                    Log.i(tag, "MusicService onUnbind()");   
                    mPlayer.stop();   
                    return false;   
            }   
       
            //该服务不存在需要被创建时被调用,不管startService()还是bindService()都会在启动时调用该方法   
            @Override   
            public void onCreate(){   
                    Toast.makeText(this, "MusicService onCreate()", Toast.LENGTH_SHORT).show();   
                    //创建一个音乐播放器对象   
                    mPlayer=MediaPlayer.create(getApplicationContext(), R.raw.babayetu);   
                    //设置可以重复播放   
                    mPlayer.setLooping(true);   
                    Log.i(tag, "MusicService onCreate()");   
            }   
       
            //用startService方法调用该服务时,在onCreate()方法调用之后,会调用改方法   
            @Override   
            public void onStart(Intent intent,int startid){   
                    Toast.makeText(this,"MusicService onStart",Toast.LENGTH_SHORT).show();   
                    Log.i(tag, "MusicService onStart()");   
                    mPlayer.start();   
            }   
       
            //该服务被销毁时调用该方法   
            @Override   
            public void onDestroy(){   
                    Toast.makeText(this, "MusicService onDestroy()", Toast.LENGTH_SHORT).show();   
                    mPlayer.stop();   
                    Log.i(tag, "MusicService onDestroy()");   
            }   
    }
复制代码
4、MainHelloService.java中的代码:   
  1. package android.basic.lesson14;   
       
    import android.app.Activity;   
    import android.content.ComponentName;   
    import android.content.Context;   
    import android.content.Intent;   
    import android.content.ServiceConnection;   
    import android.os.Bundle;   
    import android.os.IBinder;   
    import android.util.Log;   
    import android.view.View;   
    import android.view.View.OnClickListener;   
    import android.widget.Button;   
    import android.widget.Toast;   
       
    public class MainHelloService extends Activity {   
       
            //为日志工具设置标签   
            String tag = "MusicService";   
       
        /** Called when the activity is first created. */   
        @Override   
        public void onCreate(Bundle savedInstanceState) {   
            super.onCreate(savedInstanceState);   
            setContentView(R.layout.main);   
       
            //输出Toast消息和日志记录   
                    Toast.makeText(MainHelloService.this, "MainHelloService onCreate", Toast.LENGTH_SHORT).show();   
                    Log.i(tag, "MainHelloService onCreate");   
       
            //定义组件对象   
            Button b1= (Button)findViewById(R.id.Button01);   
            Button b2= (Button)findViewById(R.id.Button02);   
            Button b3= (Button)findViewById(R.id.Button03);   
            Button b4= (Button)findViewById(R.id.Button04);   
       
             //定义服务链接对象   
             final ServiceConnection conn = new ServiceConnection(){   
       
                            @Override   
                            public void onServiceConnected(ComponentName name, IBinder service) {   
                                    Toast.makeText(MainHelloService.this, "ServiceConnection onServiceConnected", Toast.LENGTH_SHORT).show();   
                                    Log.i(tag, "ServiceConnection onServiceConnected");   
       
                            }   
       
                            @Override   
                            public void onServiceDisconnected(ComponentName name) {   
                                    Toast.makeText(MainHelloService.this, "ServiceConnection onServiceDisconnected", Toast.LENGTH_SHORT).show();   
                                    Log.i(tag, "ServiceConnection onServiceDisconnected");   
       
                            }};   
       
                    //定义点击监听器   
            OnClickListener ocl= new OnClickListener(){   
       
                            @Override   
                            public void onClick(View v) {   
                                    //显示指定intent所指的对象是个Service   
                                    Intent intent = new Intent(MainHelloService.this,android.basic.lesson14.MusicService.class);   
                                    switch(v.getId()){   
                                    case R.id.Button01:   
                                            //开始服务   
                                            startService(intent);   
                                            break;   
                                    case R.id.Button02:   
                                            //停止服务   
                                            stopService(intent);   
                                            break;   
                                    case R.id.Button03:   
                                            //绑定服务   
                                            bindService(intent,conn,Context.BIND_AUTO_CREATE);   
                                            break;   
                                    case R.id.Button04:   
                                            //解除绑定   
                                            unbindService(conn);   
                                            break;   
                                    }   
                            }   
            };   
       
            //绑定点击监听器   
            b1.setOnClickListener(ocl);   
            b2.setOnClickListener(ocl);   
            b3.setOnClickListener(ocl);   
            b4.setOnClickListener(ocl);      
       
        }   
       
        @Override   
        public void onDestroy(){   
                super.onDestroy();   
                    Toast.makeText(MainHelloService.this, "MainHelloService onDestroy", Toast.LENGTH_SHORT).show();   
                    Log.i(tag, "MainHelloService onDestroy");   
        }   
    }
复制代码
  
   好了,本讲就到这里。   

上一篇:Android游戏开发之小球重力感应实现(二十五)
下一篇:第十三讲:用户界面 View(八)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

Archiver|新帖|标签|软件|Sitemap|ZNDS智能电视网 ( 苏ICP备2023012627号 )

网络信息服务信用承诺书 | 增值电信业务经营许可证:苏B2-20221768 丨 苏公网安备 32011402011373号

GMT+8, 2025-7-30 09:42 , Processed in 0.061880 second(s), 14 queries , Redis On.

Powered by Discuz!

监督举报:report#znds.com (请将#替换为@)

© 2007-2025 ZNDS.Com

快速回复 返回顶部 返回列表