android – The call recorder service application does not work, does not enter the conditional "if" to proceed with the recorder-ThrowExceptions

Exception or error:

I am doing a project to record incoming and outgoing calls without user intervention, that is to say in a Service. I’m new to programming, so look for a tutorial to take as a basis. Since the tutorial is old and android has restricted security, I have made some changes like adding administrator permission. Before that the app was closed, by adding the permission if you enter the app, but do not record anything. I put “Toast” as a test to see where it does not enter and it seems that it never enters the following IF:

if(TelephonyManager.CALL_STATE_IDLE==state && rec==null) {
                        rec.stop();
                        rec.reset();
                        rec.release();
                        recorderstarted=false;
                        stopSelf();
                        Toast.makeText(getApplicationContext(), "IF", Toast.LENGTH_SHORT).show();
                    }else if(TelephonyManager.CALL_STATE_OFFHOOK==state){
                        try {
                            rec.prepare();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        rec.start();
                        recorderstarted=true;
                        Toast.makeText(getApplicationContext(), "ELSE IF", Toast.LENGTH_SHORT).show();
                    }

Manifest:

<?xml version="1.0" encoding="utf-8"?><manifest
xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.automaticcallrecorder">
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.READ_CALL_LOG"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <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">
    <receiver
        android:name="com.example.automaticcallrecorder.DeviceAdminDemo"
        android:description="@string/ServcioDescripcion"
        android:label="deviceAdminLabel"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/my_admin" />


        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
            <action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
        </intent-filter>
    </receiver>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALLS"/>
            <action android:name="android.intent.action.PHONE_STATE"/>


            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <service android:name=".RecordingService"></service>
</application>

MainActivity:

public class MainActivity extends AppCompatActivity {
    ToggleButton startandoff;
    private static final int REQUEST_CODE = 0;
    private DevicePolicyManager mDPM;
    private ComponentName mAdminName;


@Override
protected void onCreate(Bundle savedInstanceState) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.RECORD_AUDIO },
                10);
    } else {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startandoff=findViewById(R.id.ToggleButton2);
        try {
            // Initiate DevicePolicyManager.
            mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
            mAdminName = new ComponentName(this, DeviceAdminDemo.class);

            if (!mDPM.isAdminActive(mAdminName)) {
                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
                intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Click on Activate button to secure your application.");
                startActivityForResult(intent, REQUEST_CODE);
            } else {
                //mDPM.lockNow();
                //Intent intent = new Intent(MainActivity.this,
                //TrackDeviceService.class);
                //startService(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


public void togglebutton(View view) {
    boolean checked = ((ToggleButton)view).isChecked();
    if(checked){
        Intent intent= new Intent(MainActivity.this,RecordingService.class);
        startService(intent);
        //Toast.makeText(getApplicationContext(),"1",Toast.LENGTH_SHORT).show();
    }else{
        Intent intent= new Intent(MainActivity.this,RecordingService.class);
        stopService(intent);
        Toast.makeText(getApplicationContext(),"2",Toast.LENGTH_SHORT).show();

    }
}

}

DeviceAdminDemo Class:

public class DeviceAdminDemo extends DeviceAdminReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }

    public void onEnabled(Context context, Intent intent) {
    };

    public void onDisabled(Context context, Intent intent) {
    };
}

RecordingService:

public class RecordingService extends Service {
    private MediaRecorder rec;
    private boolean recorderstarted;
    private File file;
    String path="sdcard/alarms/";

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

    @Override
    public int onStartCommand(Intent intent, int flags, final int startId) {
        //return super.onStartCommand(intent, flags, startId);
        //Toast.makeText(getApplicationContext(),"777777777777777777777777",Toast.LENGTH_SHORT).show();
        Log.d(path,"e.e");
        file= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS);
        Date date= new Date();
        CharSequence sdf = DateFormat.format("MM-dd-yy-hh-mm-ss",date.getTime());
        rec = new MediaRecorder();
        rec.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
        rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        rec.setOutputFile(file.getAbsolutePath()+"/"+"rec.3gp");
        rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

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

                    if(TelephonyManager.CALL_STATE_IDLE==state && rec==null) {
                        rec.stop();
                        rec.reset();
                        rec.release();
                        recorderstarted=false;
                        stopSelf();
                        Toast.makeText(getApplicationContext(), "IF", Toast.LENGTH_SHORT).show();
                    }else if(TelephonyManager.CALL_STATE_OFFHOOK==state){
                        try {
                            rec.prepare();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        rec.start();
                        recorderstarted=true;
                        Toast.makeText(getApplicationContext(), "ELSE IF", Toast.LENGTH_SHORT).show();
                    }
                Toast.makeText(getApplicationContext(), "FUERA", Toast.LENGTH_SHORT).show();
            }
        },PhoneStateListener.LISTEN_CALL_STATE);
        //Toast.makeText(getApplicationContext(),"5555555555555555555555",Toast.LENGTH_SHORT).show();
        return START_STICKY;
    } 
}

my_admin.xml:

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
    <uses-policies>
        <force-lock />
    </uses-policies>
</device-admin>
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *