Android development: phone recorder simulation code

The recording function and some detailed functions can be added to this framework:

 

A simple layout that puts a button:

<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"
    tools:context=".MainActivity" >



    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="57dp"
        android:text="点击开始录音"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:onClick="click"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="开始录音" />

</RelativeLayout>

 

 

 

MainActivity:

package org.dreamtech.phonelisterner;

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.activity_main);
    }

    public void click(View v) {
        Intent intent = new Intent(this, PhoneService.class);
        startService(intent);
    }
}

 

Service:

package org.dreamtech.phonelisterner;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class PhoneService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        tm.listen(new MyPhoneStateListener(),
                PhoneStateListener.LISTEN_CALL_STATE);
    }

    @Override
    public  void onDestroy () {
         super .onDestroy ();
    }

    private class MyPhoneStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:

                break ;
             case TelephonyManager.CALL_STATE_OFFHOOK:
                 // Start recording 
                break ;
             case TelephonyManager.CALL_STATE_RINGING:
                 // Prepare the recorder 
                break ;
            }
        }
    }
}

 

 

Remember to configure services and permissions (READ_CALL)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324742535&siteId=291194637