简体   繁体   English

Android-停止Telephony Manager服务

[英]Android- Stop Telephony Manager service

Actually i am new to android developer and i am having a stop button in my activity class and i want to stop the telephony service so that it will not record call after stop button is clicked but it is not stoping service and itis recording the call. 实际上,我是android开发人员的新手,我在活动类中有一个停止按钮,并且我想停止电话服务,以便单击停止按钮后它不会记录呼叫,但不会停止服务并且不会记录呼叫。 Please help to resolve the problem 请协助解决问题

My Activity class is 我的活动课是

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);

        }
    });
}
}

My CallREcorder Service 我的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;
        }
    }
}
}

My Manifest file is 我的清单文件是

<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>

You can use LISTEN_NONE as parameter to stop listening for updates. 您可以使用LISTEN_NONE作为参数来停止监听更新。

telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE );

Hope this will help you, Thanks. 希望这会对您有所帮助,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM