其实我是Android开发人员的新手,我在我的活动类中有一个停止按钮,我想停止电话服务,以便在点击停止按钮后它不会记录呼叫,但它不是停止服务而是记录电话。 请帮助解决问题
我的活动课程是
package com.jain.callrecorderdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button startbtn, stopbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
startbtn = (Button) findViewById(R.id.startbtnid);
stopbtn = (Button) findViewById(R.id.stopbtnid);
startbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(getApplicationContext(), CallRecordingService.class);
startService(in);
}
});
stopbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(getApplicationContext(), CallRecordingService.class);
stopService(in);
}
});
}
}
我的CallREcorder服务
package com.jain.callrecorderdemo;
import java.io.File;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.preference.PreferenceManager.OnActivityStopListener;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class CallRecordingService extends Service{
TelephonyManager tm;
MediaRecorder mr;
static int x=0;
int id;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
id=startId;
tm=(TelephonyManager) getSystemService(TELEPHONY_SERVICE);
tm.listen(new MyPhoneListener(), PhoneStateListener.LISTEN_CALL_STATE);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Toast.makeText(getApplicationContext(), "Call Recorder off", Toast.LENGTH_LONG).show();
super.onDestroy();
}
class MyPhoneListener extends PhoneStateListener
{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(getApplicationContext(), "IDLE", Toast.LENGTH_LONG).show();
try {
mr.release();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(getApplicationContext(), "Ringing", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(getApplicationContext(), "OFFHOOK", Toast.LENGTH_LONG).show();
try {
mr=new MediaRecorder();
mr.setAudioSource(MediaRecorder.AudioSource.MIC);
mr.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mr.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
File mysdpath=Environment.getExternalStorageDirectory();
File mydir=new File(mysdpath,"Recording_folder");
if(mydir.exists()==false)
{
mydir.mkdir();
}
String filepath=mydir.getAbsolutePath()+"/recorderfile"+(x++)+".mp3";
mr.setOutputFile(filepath);
mr.prepare();
mr.start();
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
break;
}
}
}
}
我的清单文件是
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jain.callrecorderdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<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="MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name="CallRecordingService"></service>
</application>
</manifest>
答案 0 :(得分:0)
您可以使用LISTEN_NONE作为参数来停止侦听更新。
telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE );
希望这会对你有所帮助,谢谢。