Android手机窃听器的实现

来源:互联网 发布:贪吃蛇编程代码 编辑:程序博客网 时间:2024/06/06 05:24

设置一个按钮布局点击可以开启服务。

public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void click(View v){    Intent intent=new Intent(this,PhoneService.class);    startService(intent);    }}
服务类

public class PhoneService extends Service {private MediaRecorder recorder;@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);tm.listen(new MyPhoneStateListener(),PhoneStateListener.LISTEN_CALL_STATE);super.onCreate();}private class MyPhoneStateListener extends PhoneStateListener{@Overridepublic void onCallStateChanged(int state, String incomingNumber) {switch(state){case TelephonyManager.CALL_STATE_IDLE:if(recorder!=null){ recorder.stop(); recorder.reset();   // You can reuse the object by going back to setAudioSource() step recorder.release(); // Now the object cannot be reused}break;case TelephonyManager.CALL_STATE_OFFHOOK:System.out.println("开始录"); recorder.start();   // Recording is now startedbreak;case TelephonyManager.CALL_STATE_RINGING:System.out.println("准备一个录音机");//1创建一个实例recorder = new MediaRecorder();//2设置音频的来源 recorder.setAudioSource(MediaRecorder.AudioSource.MIC);  //3设置输出的格式 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); //4设置编码方式 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //5设置有效的路径 recorder.setOutputFile("/mnt/sdcard/luyinn.3gp"); //6准备录 try {recorder.prepare();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} break;}super.onCallStateChanged(state, incomingNumber);}}}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.recorder.MainActivity" >    <Button        android:id="@+id/button1"        android:onClick="click"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_marginLeft="48dp"        android:layout_marginTop="90dp"        android:text="Button" /></RelativeLayout>

加上权限

    <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" />
注册服务

        <service android:name="com.example.recorder.PhoneService" >        </service>






0 0
原创粉丝点击