Android初试--Android中的Service简介(2)
2016-03-21 14:29
531 查看
服务的第二种启动方式: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
实例分析图
package com.click369.bindservicedemo;
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
实例分析图
package com.click369.bindservicedemo;
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();
}
}
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories