Java 为什么可以';我不能让我的电话录音应用程序工作吗?

Java 为什么可以';我不能让我的电话录音应用程序工作吗?,java,android,Java,Android,我正在尝试实现一个应用程序,允许我录制通话音频,通过切换按钮激活该服务。我无法找出我的代码有什么问题,因为一旦我将切换按钮设置为ON并启动调用,就没有保存任何文件。 我不明白问题是在保存中(可能是我在错误的目录中查找文件?),还是它实际上没有记录。 我正在使用AndroidStudio 4.0,并在我的三星S9上尝试我的应用程序 感谢安德万斯的帮助 RecordService.java package com.example.registrachiamate; import android.a

我正在尝试实现一个应用程序,允许我录制通话音频,通过切换按钮激活该服务。我无法找出我的代码有什么问题,因为一旦我将切换按钮设置为ON并启动调用,就没有保存任何文件。 我不明白问题是在保存中(可能是我在错误的目录中查找文件?),还是它实际上没有记录。 我正在使用AndroidStudio 4.0,并在我的三星S9上尝试我的应用程序

感谢安德万斯的帮助

RecordService.java

package com.example.registrachiamate;

import android.app.MediaRouteButton;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.format.DateFormat;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class RecordService extends Service {
    private MediaRecorder rec;
    private File file;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //return super.onStartCommand(intent, flags, startId);
        file= Environment.getExternalStorageDirectory();
        Date date=new Date();
        CharSequence sdf= DateFormat.format("MM-dd-yy-hh-mm--ss",date.getTime());
        rec=new MediaRecorder();
        rec.setAudioSource(MediaRecorder.AudioSource.MIC);
        rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        rec.setOutputFile(file.getAbsolutePath()+"/"+sdf+"rec.3gp");
        rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        TelephonyManager manager=(TelephonyManager) getApplicationContext().getSystemService(getApplicationContext().TELEPHONY_SERVICE);
        assert manager != null;
        manager.listen(new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, String phoneNumber) {
                //super.onCallStateChanged(state, phoneNumber) {

                if (TelephonyManager.CALL_STATE_IDLE==state){

                rec.stop();
                rec.reset();
                 rec.release();
                    stopSelf();
                }else if(TelephonyManager.CALL_STATE_OFFHOOK==state && rec==null){

                    try {
                        rec.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    rec.start();


                }

                }

        },PhoneStateListener.LISTEN_CALL_STATE);

        return START_STICKY;
    }
}
ActivityButton1.java

package com.example.registrachiamate;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;

public class ActivityButton1 extends AppCompatActivity {
    ToggleButton startandoff;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button1);
        startandoff=(ToggleButton)findViewById(R.id.toggleButton);


    }

   public void tooglebutton(View view)
   {
       boolean checked=((ToggleButton)view).isChecked();
       if (checked){

           Intent intent=new Intent(this,RecordService.class);
           startService(intent);
           Toast.makeText(getApplicationContext(),"Call Record STARTED",Toast.LENGTH_SHORT).show();

       }else {

           Intent intent=new Intent(this,RecordService.class);
           stopService(intent);
           Toast.makeText(getApplicationContext(),"Call Record STOPPED",Toast.LENGTH_SHORT).show();


   }
}
}

AndroidManifest.xml,如果我将RecordService放入其中,它将停止运行

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.registrachiamate">
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
                <action android:name="android.intent.action.PHONE_STATE"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <activity android:name=".ActivityButton1"></activity>
        <activity android:name=".ActivityButton2"></activity>
        <activity android:name=".ActivityButton3"></activity>
        <activity android:name=".ActivityButton4"></activity>
        <activity android:name=".RecordService"></activity>
    </application>

</manifest>