这是我的代码:
package com.callplus;
import java.io.IOException;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class AudioRecordTest extends Service {
private static String TAG = "AudioRecordTest";
private static String FileName = null;
private MediaRecorder Recorder = null;
public static String callerName, date, path,number;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "I am in Oncreate");
DBAdapter db = new DBAdapter(this);
db.open(SQLiteDatabase.NO_LOCALIZED_COLLATORS);
db.getPath();
path = db.audioPath;
db.close();
final TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener() {
public void onCallStateChanged(final int state,
final String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
if (Recorder != null) {
Recorder.release();
Recorder.stop();
}
Log.d("TestActivity", "Call is idle.");
actionStop(getApplicationContext());
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("TestActivity", "Call connected");
startRecording();
break;
case TelephonyManager.CALL_STATE_RINGING:
break;
default:
break;
}
} finally {
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy()
{
super.onDestroy();
stopSelf();
}
public static void actionStop(Context ctx)
{
Intent i = new Intent(ctx, AudioRecordTest.class);
Log.d(TAG, "I am in ActionStop");
// i.setAction(ACTION_STOP);
ctx.startService(i);
}
}
我想在呼叫空闲时停止服务。
答案 0 :(得分:1)
在actionStop方法而不是stopService方法调用中,您再次调用了startService方法。
ctx.stopService(i);
使用上面的行而不是那个并且回复响应