Startservice与Bindservice的使用
Startservice
① starservice启动服务的特点:可以看作为没有界面的activity,可以进行后台运行,主要的使用场景为wifi下自动更新软件,音乐的后台播放,即时通信软件的消息推送等
② startservice的使用方法:创建一个类,继承Service并重写里面的生命周期方法
③ 注意:多次调用startservice时,oncreate只会执行一次,onstartcommand会执行多次,需要停止startservice时,使用stopservice,stopservice可以多次调用,但是,只有第一次有效,会执行ondestroy把服务销毁.
④ startservice开启的服务,与开启它的组件没有关系,会长期在后台运行,只要用户不手动停止服务,服务就会一直运行下去,只有当系统内存不足的是否,才有可能会被系统回收
电话录音机案例
代码示例
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void open(View view) {
Intent intent = new Intent(this,MyService.class);
startService(intent);
}
}
package com.example.day15;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
//创建服务
@Override
public void onCreate() {
super.onCreate();
//获取TelephonyManager对象,监听打电话状态的改变
TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
MyPhoneListener listener = new MyPhoneListener();
//创建一个监听器对象,
manager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
}
//开启服务
@Override
public ComponentName startService(Intent service) {
return super.startService(service);
}
//销毁服务
@Override
public void onDestroy() {
super.onDestroy();
}
//创建一个内部类,监听打电话的状态
public class MyPhoneListener extends PhoneStateListener{
private MediaRecorder recorder;
private final String TAG = this.getClass().getSimpleName();
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state){
//电话处于挂断状态
case TelephonyManager.CALL_STATE_IDLE:
Log.e(TAG,"电话处于挂断状态");
if (recorder!=null){
recorder.stop();
recorder.reset();//重置录音
recorder.release();//解除对录音资源的占用
}
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.e(TAG,"电话处于响铃状态");
//准备一个录音机
recorder = new MediaRecorder();
//设置音频源
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置保存的格式
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//设置编码器
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//设置存放的路径
String path = getFilesDir().getAbsolutePath()+"/"+incomingNumber+"3gp";
recorder.setOutputFile(path);
//开始准备录音
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.e(TAG,"电话处于接听状态");
//开始录音
if (recorder!=null){
recorder.start();
}
break;
}
}
}
}
bindservice
① bindservice开启的服务,通过unbindservice关闭,方法只能调用一次,第一次调用可以关闭服务,多次调用会抛出异常
② bindservice和开启它的组件建立了绑定关系,不求同生,但求同死
③ activity通过bindservice开启了一个服务,关闭的时候必须解除绑定
④ bindservice开启的服务不是后台服务,当开启它的组件销毁时,它也会销毁
⑤ 只有用startservice开启的服务,才会让当前的进程处于服务进程状态
通过bindservice调用服务中的方法##
代码示例
activity
public class MainActivity extends AppCompatActivity {
private Myconn myconn;
private MyService.MyBinder binder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//绑定服务
public void bind(View view) {
Intent intent = new Intent(this,MyService.class);
myconn = new Myconn();
bindService(intent,myconn,BIND_AUTO_CREATE);
}
public class Myconn implements ServiceConnection{
private final String TAG = this.getClass().getSimpleName();
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e(TAG,"onServiceConnected");
binder = (MyService.MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e(TAG,"onServiceDisconnected");
}
}
public void unbind(View view) {
unbindService(myconn);
}
public void call(View view) {
binder.callMethod("hello");
}
}
Myservice
public class MyService extends Service {
private final String TAG = this.getClass().getSimpleName();
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG,"onBind");
return new MyBinder();
}
public class MyBinder extends Binder{
public void callMethod(String s){
method(s);
}
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG,"onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.e(TAG,"onDestroy");
super.onDestroy();
}
public void method(String s){
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
}
}
音乐播放器框架
代码示例
activity
package com.example.it;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private final String TAG = this.getClass().getSimpleName();
private MyService.MyBinder myBinder;
private Myconn myconn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,MyService.class);
myconn = new Myconn();
bindService(intent,myconn,BIND_AUTO_CREATE);
startService(intent);
}
public void top(View view) {
myBinder.playtop();
}
public void play(View view) {
myBinder.play();
}
public void stoop(View view) {
myBinder.stop();
}
public void next(View view) {
myBinder.playnext();
}
public class Myconn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e(TAG,"打开播放器");
myBinder = (MyService.MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e(TAG,"退出播放器");
}
}
}
Myservice
package com.example.it;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private final String TAG = this.getClass().getSimpleName();
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG,"音乐播放器 onBind");
return new MyBinder();
}
public class MyBinder extends Binder{
public void playtop(){
Log.e(TAG,"播放上一首");
}
public void playnext(){
Log.e(TAG,"播放下一首");
}
public void play(){
Log.e(TAG,"播放");
}
public void stop(){
Log.e(TAG,"暂停");
}
}
@Override
public void onCreate() {
Log.e(TAG,"创建一个播放器");
super.onCreate();
}
@Override
public void onDestroy() {
Log.e(TAG,"退出播放器");
super.onDestroy();
}
}