0
其實我對android開發人員是新手,我在我的活動類中有一個停止按鈕,我想停止電話服務,以便它在停止按鈕被單擊後不會記錄呼叫,但它是沒有停止服務,它記錄了通話。 請幫忙解決問題Android-停止電話管理器服務
我Activity類是
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>
你有沒有在清單文件中寫過的東西。請發佈您的清單文件。 –
我已添加艙單也.. –