Service 服务
清单文件中_注册:
<application 层中
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service android:name="MyService"></service>
第一次写的服务:MyService.java
package com.bz.service;
import java.text.SimpleDateFormat;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Binder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class MyService extends Service {
@Override
public void onCreate(){
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
tm.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy(){
super.onDestroy();
}
private MediaRecorder recorder = null;
private class MyPhoneStateListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
if(recorder!=null){
recorder.stop();
recorder.reset();
recorder.release();
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
recorder.start();
break;
case TelephonyManager.CALL_STATE_RINGING:
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
String str = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new java.util.Date());
recorder.setOutputFile("/mnt/sdcard2/recording_"+str+".3gp");
try {
recorder.prepare();
} catch (Exception e) {
e.printStackTrace();
}
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
@Override
public IBinder onBind(Intent intent){
return new MyBinder();
}
public void Tosat(){
Toast.makeText(getApplicationContext(), "界面调用服务的方法", 0).show();
}
private class MyBinder extends Binder implements IMyBinder{
public void goTosat(){Tosat();}
}
}
服务中间人(接口对象):IMyBinder.java
package com.bz.service;
public interface IMyBinder {
public void goTosat();
}
开启服务
Intent intent = new Intent(this, MyService.class);
startService(intent);
中间人
IMyBinder myBinder;
private class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("服务绑定成功");
myBinder = (IMyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("服务解绑定成功");
}
}
停止服务
Intent intent = new Intent(this, MyService.class);
stopService(intent);
..
/*
* 调用别的App的服务的方法: (如支付宝的支付)
*
* 拥有服务的App:
* 1.重命名暴露方法的借口 (本App中的IMyBinder.java), 重命名为.aidl (如IMyBinder.aidl), 去除所以public (因为aidl所有本来就是公有的,不需要加)
* 2.服务中的子类 (MyBinder implements IMyBinder) 改为接口Stub (系统自动生成的,MyBinder implements Stub)
*
* 要调用服务的App:
* 1.创建一个名称‘有服务的包’一模一样的包(com.bz.service),把上面的.aidl文件复制到包里
* 2.绑定服务的方法为:Intent i = new Intent(); /n intent.setAction("上面App清单文件中服务的名称'com.bz.service.MyBootReceiver'"); bindService(i,new MyConn(), BIND_AUTO_CREATE);
* 3.也要在清单文件注册这个服务哦 (com.bz.service.MyBootReceiver)
* 4.获取中间人的方法改为:中间人对象 = Stub.asInterface(service);
*
*/