原理图:
代码如下:
1、main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="startservice1" android:text="监听电话呼叫状态" /> </LinearLayout>
2、MainActivity
package com.njupt.phoneservice1; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void startservice1(View v){ Intent intent = new Intent(this,PhoneService1.class); startService(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
3、PhoneService
package com.njupt.phoneservice1; import java.io.File; import java.text.SimpleDateFormat; import android.app.Service; import android.content.Context; import android.content.Intent; import android.media.MediaRecorder; import android.os.Environment; import android.os.IBinder; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; public class PhoneService1 extends Service { private TelephonyManager tm; private MyPhoneStateListener1 listener; private MediaRecorder mr; @Override public void onCreate() { super.onCreate(); tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); listener = new MyPhoneStateListener1(); tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); } private class MyPhoneStateListener1 extends PhoneStateListener { public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_IDLE: System.out.println("闲置状态1"); if (mr != null) { // if(incomingNumber.equals("5556")||incomingNumber.equals("15555215556")){ mr.stop(); mr.reset(); mr.release(); mr = null; // } } break; case TelephonyManager.CALL_STATE_OFFHOOK: System.out.println("接听状态1"); try { mr = new MediaRecorder(); mr.setAudioSource(MediaRecorder.AudioSource.MIC); mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // String name = format.format(System.currentTimeMillis()); File file = new File( Environment.getExternalStorageDirectory(), System.currentTimeMillis()+ ".3gp"); mr.setOutputFile(file.getAbsolutePath()); mr.prepare(); mr.start(); } catch (Exception e) { e.printStackTrace(); } break; case TelephonyManager.CALL_STATE_RINGING: System.out.println("响铃状态1"); break; default: break; } }; } @Override public IBinder onBind(Intent intent) { return null; } }
4、AndroidManifest.xml
注册service组件及所需要的权限
权限
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> //用来读取电话状态 <uses-permission android:name="android.permission.RECORD_AUDIO"/>//录音的相关权限 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>//以下两个是读写SDcard相应的权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
注册组件
<service android:name="com.njupt.phoneservice1.PhoneService1"/>