当前位置: 首页 > 知识库问答 >
问题:

如何在SD卡中存储通话记录?

陈增
2023-03-14

我正在尝试将呼入和呼出记录存储在我的SD卡中。但问题是我无法在我的SD卡中找到它。以下是我的代码,任何人都可以帮助我吗?

设备管理员演示.java

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

        context.stopService(new Intent(context, TService.class));
        Intent myIntent = new Intent(context, TService.class);
        context.startService(myIntent);

    }

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

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

TService.java

public class TService extends Service {
    MediaRecorder recorder;
    File audiofile;
    String name, phonenumber;
    String audio_format;
    public String Audio_Type;
    int audioSource;
    Context context;

    Timer timer;
    Boolean offHook = false, ringing = false;
    Toast toast;
    Boolean isOffHook = false;
    private boolean recordstarted = false;

    private static final String ACTION_IN = "android.intent.action.PHONE_STATE";
    private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL";
    private CallBr br_call;




    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onDestroy() {
        Log.d("service", "destroy");

        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // final String terminate =(String)
        // intent.getExtras().get("terminate");//
        // intent.getStringExtra("terminate");
        // Log.d("TAG", "service started");
        //
        // TelephonyManager telephony = (TelephonyManager)
        // getSystemService(Context.TELEPHONY_SERVICE); // TelephonyManager
        // // object
        // CustomPhoneStateListener customPhoneListener = new
        // CustomPhoneStateListener();
        // telephony.listen(customPhoneListener,
        // PhoneStateListener.LISTEN_CALL_STATE);
        // context = getApplicationContext();

        final IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_OUT);
        filter.addAction(ACTION_IN);
        this.br_call = new CallBr();
        this.registerReceiver(this.br_call, filter);

        // if(terminate != null) {
        // stopSelf();
        // }
        return START_NOT_STICKY;
    }

    public class CallBr extends BroadcastReceiver {
        Bundle bundle;
        String state;
        String inCall, outCall;
        public boolean wasRinging = false;

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ACTION_IN)) {
                if ((bundle = intent.getExtras()) != null) {
                    state = bundle.getString(TelephonyManager.EXTRA_STATE);
                    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                        inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                        wasRinging = true;
                        Toast.makeText(context, "IN : " + inCall, Toast.LENGTH_LONG).show();
                    } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                        if (wasRinging == true) {

                            Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG).show();

                            String out = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss").format(new Date());
                            File sampleDir = new File(Environment.getExternalStorageDirectory(), "/TestRecordingDasa1");
                            if (!sampleDir.exists()) {
                                sampleDir.mkdirs();
                            }
                            String file_name = "Record";
                            try {
                                audiofile = File.createTempFile(file_name, ".amr", sampleDir);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            String path = Environment.getExternalStorageDirectory().getAbsolutePath();
                            System.out.println("PATH"+path);

                            recorder = new MediaRecorder();
//                          recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);

                            recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
                            recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
                            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                            recorder.setOutputFile(audiofile.getAbsolutePath());
                            try {
                                recorder.prepare();
                            } catch (IllegalStateException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            recorder.start();
                            recordstarted = true;
                        }
                    } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                        wasRinging = false;
                        Toast.makeText(context, "REJECT || DISCO", Toast.LENGTH_LONG).show();
                        if (recordstarted) {
                            recorder.stop();
                            recordstarted = false;
                        }
                    }
                }
            } else if (intent.getAction().equals(ACTION_OUT)) {
                if ((bundle = intent.getExtras()) != null) {
                    outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                    Toast.makeText(context, "OUT : " + outCall, Toast.LENGTH_LONG).show();
                }
            }
        }
    }

}

共有1个答案

常鸿朗
2023-03-14

更新的答案

Main2Activity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
}

收听来电.java

public class ListenIncomingCalls extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        TelephonyManager tmgr = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        CallListener phoneListener = new CallListener();
        tmgr.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    }
}

class CallListener extends PhoneStateListener {

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        System.out.println("Call state changed ");
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            //phone ringing
            System.out.println("This number is " + incomingNumber);
        }
        if(state == TelephonyManager.CALL_STATE_OFFHOOK)
        {
            System.out.println("Call is picked ");
            //start CallRecordService Here to start recording
        }
    }


}

CallRecordService.java

public class CallRecordService extends Service {

    MRecorder mRecorder;
    long passedSeconds = 0;
    NotificationCompat.Builder mBuilder;
    NotificationManager mNotificationManager;

    public CallRecordService()
    {
        mRecorder = MRecorder.getInstance();
    }




    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("Service binded");
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("Service called ");

        mRecorder.startRecording();

        Intent stopService = new Intent(this,StopPhoneReciever.class);
        stopService.putExtra("stopRecording",true);
        PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),68,stopService,PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setOngoing(true)
                .setContentIntent(pendingIntent)
                .setContentTitle("Recording Call...")
                .setContentText("Tap to stop recording.....");
        Notification notification = mBuilder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(68,notification);
    }

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

}

Manifest.xml

 <!--incoming calls receiver -->
        <receiver android:name=".recorder.ListenIncomingCalls">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
        </receiver>
<service android:name=".recorder.CallRecordService" />

很少有用的链接
1)https://www.codeproject.com/Articles/548416/Detecting-incoming-and-outgoing-phone-calls-on-And
2)http://androidexample.com/Incomming_Phone_Call_Broadcast_Receiver__-_Android_Example/index.php?view=article_discription

 类似资料:
  • 问题内容: 我在SD卡中有一个文件夹,其中包含几个文件。现在我需要获取该文件的名称。有人能知道如何获取存储在SD卡中的文件名吗? 任何帮助将是感激的。非常感谢。 问题答案: 将给您对应的SDCARD。然后,您只需要使用方法。 那应该是这样的: 注意事项:来自KitKat及以上版本,需要获得许可。

  • SD卡设备的使用 SD卡设备的使用 源码/* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2018-09-25 misonyo first edition. *//* * 程序清

  • 我正在尝试启动来打开应用程序中资产文件夹中的pdf。我已经读了几十个帖子,但仍然卡住了。显然,我需要先将pdf复制到sd卡,然后启动。还是不起作用。 我认为问题是启动,所以我只是尝试打开一个文件“example.pdf”,我使用以下代码复制到SD卡上: 这是我的LogCat输出。 除了在运行此代码时,我得到以下吐司(不是由我的应用程序生成的) “不支持的文档类型” 但是我可以通过安装的PDF查看应

  • 当我运行progressdialog出现在屏幕上时,它突然停止运行,并发出通知“应用程序停止了,再试一次”,然后我在android manitor上重复了这个错误,当然我首先要做的是:

  • 对于以前的电话(Android4.0),我写了一个应用程序,使用数据文件,我复制到SD卡从PC机,使用USB电缆。该应用程序使用Environment.getExternalStorageDirectory()来检索文件,这是许多站点所建议的。 还有一个辅助问题:getExternalStorageDirectory()函数不获取外部存储目录有什么意义?