gpt4 book ai didi

java - 在我的应用程序中,我需要记录每个来电和去电

转载 作者:行者123 更新时间:2023-11-30 03:11:32 33 4
gpt4 key购买 nike

<分区>

接收类代码如下

package com.example.crecording;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IInterface;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class callReceiver extends BroadcastReceiver {

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

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

}

class phoneListener extends PhoneStateListener{
private Context context;
phoneListener(Context c){
super();
context=c;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
SharedPreferences preferences = context.getSharedPreferences("CallReceiver", Context.MODE_PRIVATE);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE :
Toast.makeText(context, state+"",Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:

String phone_number = preferences.getString("phone_number",null);
Intent serv = new Intent(context,
cRecordingservice.class);
serv.putExtra("number", phone_number);
context.startService(serv);

Toast.makeText(context, state+"",Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_RINGING :
SharedPreferences.Editor editor = preferences.edit();
editor.putString("phone_number", incomingNumber);
editor.commit();
Toast.makeText(context, state+""+incomingNumber,Toast.LENGTH_LONG).show();
break;

}
}
}
}

通话录音服务代码如下

    package com.example.crecording;

import java.io.File;
import java.io.IOException;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class cRecordingservice extends Service {
MediaRecorder callrecorder;
Boolean recording;
MainActivity main=new MainActivity();
BroadcastReceiver myreceiver=new BroadcastReceiver(){

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

myphonestatelistener mpl=new myphonestatelistener(context);
TelephonyManager tm=(TelephonyManager)context.getSystemService(TELEPHONY_SERVICE);
tm.listen(mpl,PhoneStateListener.LISTEN_CALL_STATE);

}

class myphonestatelistener extends PhoneStateListener{
private Context context;

myphonestatelistener(Context c){
context=c;
}

@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);

switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
if(recording){
stopRecording();
Toast.makeText(context, "stopped", Toast.LENGTH_SHORT).show();

}

break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(!recording){
startRecording();
Toast.makeText(context, "started", Toast.LENGTH_SHORT).show();
}
break;
case TelephonyManager.CALL_STATE_RINGING:

break;


}
}



}
};



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

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(getApplicationContext(), "Service Created", Toast.LENGTH_LONG).show();

IntentFilter RecFilter = new IntentFilter();
RecFilter.addAction("android.intent.action.PHONE_STATE");
RecFilter.addAction("android.intent.action.NEW_OUTGOING_CALL");
registerReceiver(myreceiver, RecFilter);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(myreceiver);
Toast.makeText(getApplicationContext(), "Service destroyed", Toast.LENGTH_LONG).show();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
// startRecording();
return START_STICKY;


}



void startRecording(){
String root="/sdcard/";
String fname="rec"+System.currentTimeMillis()+".amr";
File file=new File(root,fname);
callrecorder=new MediaRecorder();
callrecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
callrecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
callrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
callrecorder.setOutputFile(file.getAbsolutePath()
);
try {
callrecorder.prepare();

callrecorder.start();
recording=true;
// Toast.makeText(getApplicationContext(), "Started", Toast.LENGTH_SHORT).show();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
// Toast.makeText(getApplicationContext(),e.printStackTrace(),3000 ).show();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


void stopRecording(){

callrecorder.stop();
// callrecorder.release();
callrecorder.reset();
recording=false;

}





}

主 Activity .java

public class MainActivity extends Activity {
Intent intent2;
static MediaRecorder callrecorder;
static Boolean recording=false;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnStart=(Button)findViewById(R.id.button1);
Button btnStop=(Button)findViewById(R.id.button2);
btnStart.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Start Service", 10000).show();
intent2 = new Intent(getApplicationContext(), cRecordingservice.class);
// getApplicationContext().startService(intent2);

}
});

btnStop.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Stop Service", 10000).show();
intent2 = new Intent(getApplicationContext(), cRecordingservice.class);
getApplicationContext().stopService(intent2);
}
});
}

}

下面是 list 代码

<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.crecording.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.crecording.callReceiver" >
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
<service android:name="com.example.crecording.cRecordingservice" ></service>
</application>

更改后应用程序能够记录一个电话,但之后它会生成无法识别格式的文件并将文件附加到当前文件。我不知道为什么会这样

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com