Android初试--Android中的Service简介(2)
来源:互联网 发布:系统还原数据恢复 编辑:程序博客网 时间:2024/05/29 10:02
服务的第二种启动方式:context.bindService()
context.bindService()的启动流程:
context.bindService()-----》onCreate()-----》onBind()-----Service running
onBind()将返回给Activity一个IBind接口对象,在Activity中我们通过ServiceConnection接口中的
onServiceConnected(ComponentName name, IBinder service) 的第二个参数,在Activity里面得到IBind接口对象。
然后我们可以使用,在Activity里面得到IBind接口对象,得到Service类的对象。
Context.unbindService()-----》onUnbind() ---》 onDestroy()---Service stop
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.view.View;
import android.widget.Button;
public class MainActivity extends Activity{
private ServiceConnection serviceConnection=null;
private MyService myService=null;
public class MyServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName arg0, IBinder ibinder) {
myService=((MyService.MyIBinder)ibinder).getService();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
myService=null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//启动服务[绑定服务]
Intent intentservice=new Intent(this,MyService.class);
serviceConnection=new MyServiceConnection();
this.bindService(intentservice, serviceConnection, Context.BIND_AUTO_CREATE);
}
public void playAndPause(View view){
Button button=(Button)view;
String button_value=button.getText().toString();
if(button_value.equals("播放音乐")){
button.setText("暂停播放");
myService.play();
}else if(button_value.equals("暂停播放")){
button.setText("播放音乐");
myService.pause();
}
}
public void stopMyService(View view){
this.unbindService(serviceConnection);
}
public void exitApp(View view){
this.finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
this.unbindService(serviceConnection);
}
}
package com.click369.bindservicedemo;
import java.io.File;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
public class MyService extends Service {
private MediaPlayer mediaPlayer=null;
private MyIBinder myIBinder=null;
public class MyIBinder extends Binder{
public MyService getService(){
return MyService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
myIBinder=new MyIBinder();
if(mediaPlayer==null){
mediaPlayer=new MediaPlayer();
File path=new File(Environment.getExternalStorageDirectory(),"xinyu.mp3");
try {
mediaPlayer.setDataSource(path.getPath());
mediaPlayer.prepare();
mediaPlayer.setLooping(false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public IBinder onBind(Intent intent) {
return this.myIBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
if(mediaPlayer!=null || mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
}
}
public void play(){
if(mediaPlayer!=null && !mediaPlayer.isPlaying()){
//播放
mediaPlayer.start();
}
}
public void pause() {
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
}
context.bindService()的启动流程:
context.bindService()-----》onCreate()-----》onBind()-----Service running
onBind()将返回给Activity一个IBind接口对象,在Activity中我们通过ServiceConnection接口中的
onServiceConnected(ComponentName name, IBinder service) 的第二个参数,在Activity里面得到IBind接口对象。
然后我们可以使用,在Activity里面得到IBind接口对象,得到Service类的对象。
Context.unbindService()-----》onUnbind() ---》 onDestroy()---Service stop
实例分析图
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.view.View;
import android.widget.Button;
public class MainActivity extends Activity{
private ServiceConnection serviceConnection=null;
private MyService myService=null;
public class MyServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName arg0, IBinder ibinder) {
myService=((MyService.MyIBinder)ibinder).getService();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
myService=null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//启动服务[绑定服务]
Intent intentservice=new Intent(this,MyService.class);
serviceConnection=new MyServiceConnection();
this.bindService(intentservice, serviceConnection, Context.BIND_AUTO_CREATE);
}
public void playAndPause(View view){
Button button=(Button)view;
String button_value=button.getText().toString();
if(button_value.equals("播放音乐")){
button.setText("暂停播放");
myService.play();
}else if(button_value.equals("暂停播放")){
button.setText("播放音乐");
myService.pause();
}
}
public void stopMyService(View view){
this.unbindService(serviceConnection);
}
public void exitApp(View view){
this.finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
this.unbindService(serviceConnection);
}
}
package com.click369.bindservicedemo;
import java.io.File;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
public class MyService extends Service {
private MediaPlayer mediaPlayer=null;
private MyIBinder myIBinder=null;
public class MyIBinder extends Binder{
public MyService getService(){
return MyService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
myIBinder=new MyIBinder();
if(mediaPlayer==null){
mediaPlayer=new MediaPlayer();
File path=new File(Environment.getExternalStorageDirectory(),"xinyu.mp3");
try {
mediaPlayer.setDataSource(path.getPath());
mediaPlayer.prepare();
mediaPlayer.setLooping(false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public IBinder onBind(Intent intent) {
return this.myIBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
if(mediaPlayer!=null || mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
}
}
public void play(){
if(mediaPlayer!=null && !mediaPlayer.isPlaying()){
//播放
mediaPlayer.start();
}
}
public void pause() {
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
}
0 0
- Android初试--Android中的Service简介(2)
- Android初试--Android中的Service简介(1)
- Android初试--Android中的ContentProvider(2)
- Android初试--Android中的BroadcastReceiver(2)
- Android初试--Android中的文件操作之SDcard操作(2)
- Android初试--Android中的样式与主题
- Android初试--Android中的ContentProvider(1)
- Android初试--Android中的ContentProvider(3)
- Android初试--Android中的BroadcastReceiver(1)
- Android初试--Android中的BroadcastReceiver(3)
- 初试Android
- Android初试
- android service简介
- android service简介 .
- android Service简介
- Android Service 简介
- Android:Service简介
- Android基础service简介
- 解决tomcat报错memory leak问题
- Linux 下使用USB 网络
- android graphic(14)—EGL和OpenGL ES之间的关系
- CCS3.3 IO文件增加探针并显示图形的方法
- 架构设计:生产者/消费者模式 第3页:队列缓冲区
- Android初试--Android中的Service简介(2)
- 关于USB OTG HNP和SRP的基本概念
- 2016年第七届蓝桥杯C/C++程序设计本科B组省赛 交换瓶子(编程大题)
- BC#76 C_HDU5647 DZY Loves Connecting 树形DP
- java常见内存溢出(OOM)解决方案
- ACM练习笔记
- 深入理解 GCD(二)
- 等等 python 爬虫
- 【Android 总结】Android 知识点