Service
1.启动服务
<Button
android:id="@+id/startservice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="启动服务" />
package com.lgl.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
/**
* 我的服务
* Created by lgl on 16/4/10.
*/
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
<service android:name=".MyService"/>
case R.id.startservice:
Intent i = new Intent(this, MyService.class);
//启动服务
startService(i);
break;
2.停止服务
<Button
android:id="@+id/stopservice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务" />
case R.id.stopservice:
Intent intent = new Intent(this, MyService.class);
//停止服务
stopService(intent);
break;
3.声明周期
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
4.通话录音机
@Override
public void onCreate() {
super.onCreate();
getService();
}
public void getService() {
//服务一创建就监听
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
tm.listen(new myListener(), PhoneStateListener.LISTEN_CALL_STATE);
}
class myListener extends PhoneStateListener {
//电话状态改变的回调
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
//推断当前的状态
switch (state) {
//电话空暇
case TelephonyManager.CALL_STATE_IDLE:
Log.i("myListener", "电话空暇");
break;
//电话响铃
case TelephonyManager.CALL_STATE_RINGING:
Log.i("myListener", "电话响铃");
break;
//正在通话
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("myListener", "电话通话");
break;
}
}
}
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
//电话响铃
case TelephonyManager.CALL_STATE_RINGING:
Log.i("myListener", "电话响铃");
//推断硬件是否支持录音
if (recorder == null) {
getRecorder();
}
break;
/**
* 通话录音
*/
private void getRecorder() {
recorder = new MediaRecorder();
//麦克风
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置格式 3GP
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//设置保存文件夹 权限
recorder.setOutputFile("sdcard/audio.3gp");
//设置音频编码
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
//准备录音
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
//正在通话
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("myListener", "电话通话");
if(recorder != null){
//開始录音
recorder.start();
}
break;
//电话空暇
case TelephonyManager.CALL_STATE_IDLE:
Log.i("myListener", "电话空暇");
if(recorder != null){
//停止录音
recorder.stop();
//释放资源
recorder.release();
recorder = null;
}
break;
MyService
package com.lgl.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.io.IOException;
/**
* 电话录音
* Created by lgl on 16/4/10.
*/
public class MyService extends Service {
//录音
private MediaRecorder recorder;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
getService();
}
/**
* 获取电话状态
*/
public void getService() {
//服务一创建就监听
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
tm.listen(new myListener(), PhoneStateListener.LISTEN_CALL_STATE);
}
class myListener extends PhoneStateListener {
//电话状态改变的回调
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
//推断当前的状态
switch (state) {
//电话空暇
case TelephonyManager.CALL_STATE_IDLE:
Log.i("myListener", "电话空暇");
if(recorder != null){
//停止录音
recorder.stop();
//释放资源
recorder.release();
recorder = null;
}
break;
//电话响铃
case TelephonyManager.CALL_STATE_RINGING:
Log.i("myListener", "电话响铃");
//推断硬件是否支持录音
if (recorder == null) {
getRecorder();
}
break;
//正在通话
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("myListener", "电话通话");
if(recorder != null){
//開始录音
recorder.start();
}
break;
}
}
}
/**
* 通话录音
*/
private void getRecorder() {
recorder = new MediaRecorder();
//麦克风
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置格式 3GP
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//设置保存文件夹 权限
recorder.setOutputFile("sdcard/audio.3gp");
//设置音频编码
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
//准备录音
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
Demo下载:http://download.csdn.net/detail/qq_26787115/9486846
5.Service的两种启动方式
activity_main.xml
<?xml version="1.0" encoding="utf-8"?
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btn_start"
android:text="启动服务"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_stop"
android:text="停止服务"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_bind"
android:text="绑定服务"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_unbind"
android:text="解除绑定"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity
package com.lgl.servicetwo;
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.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_start, btn_stop, btn_bind, btn_unbind;
private MyConn conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
conn = new MyConn();
}
private void initView() {
btn_start = (Button) findViewById(R.id.btn_start);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_bind = (Button) findViewById(R.id.btn_bind);
btn_unbind = (Button) findViewById(R.id.btn_unbind);
btn_start.setOnClickListener(this);
btn_stop.setOnClickListener(this);
btn_bind.setOnClickListener(this);
btn_unbind.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_start:
startService(new Intent(this,OneService.class));
break;
case R.id.btn_stop:
stopService(new Intent(this,OneService.class));
break;
case R.id.btn_bind:
bindService(new Intent(this,OneService.class),conn,BIND_AUTO_CREATE);
break;
case R.id.btn_unbind:
unbindService(conn);
break;
}
}
/**
* 服务回调
*/
class MyConn implements ServiceConnection{
//到服务的连接被建立了
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
//到服务的连接中断了
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
OneService
package com.lgl.servicetwo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
/**
* Created by lgl on 16/4/10.
*/
public class OneService extends Service {
//绑定
@Override
public IBinder onBind(Intent intent) {
return null;
}
//解绑
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
//创建
@Override
public void onCreate() {
super.onCreate();
}
//開始
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
//销毁
@Override
public void onDestroy() {
super.onDestroy();
}
}
startService:通过startService启动的服务。该服务所在的进程会变成服务进程
bindSevice:进程优先级不变,可是绑定的服务和activity同生共死
6.调用service的方法
//服务方法
public void isShow(){
System.out.print("我是服务里的一个方法");
}
<Button
android:id="@+id/btn_isshow"
android:text="调用方法"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
TwoService
package com.lgl.servicetwo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
/**
* Created by lgl on 16/4/10.
*/
public class TwoService extends Service {
@Override
public IBinder onBind(Intent intent) {
//返回IBinder对象
return new Onclick();
}
/**
* 中间衔接
*/
class Onclick extends Binder {
public void getPic() {
isShow();
}
}
//服务方法
public void isShow() {
Log.i("TwoService","我是服务里的一个方法");
}
}
Intent i = new Intent(this,TwoService.class);
//假设须要解绑的话就不能用匿名内部类
bindService(i, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//就是返回的对象
onclick = (TwoService.Onclick) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}, BIND_AUTO_CREATE);
case R.id.btn_isshow:
onclick.getPic();
break;
7.方法抽调成接口
package com.lgl.servicetwo;
/**
* 接口
* Created by lgl on 16/4/12.
*/
public interface PublicGG {
void getPic();
}
/**
* 中间衔接
*/
class Onclick extends Binder implements PublicGG{
@Override
public void getPic() {
isShow();
}
}
PublicGG pg;