Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在接到来电时暂停服务,并在通话结束后恢复服务?_Android_Android Service_Phone State Listener - Fatal编程技术网

Android 如何在接到来电时暂停服务,并在通话结束后恢复服务?

Android 如何在接到来电时暂停服务,并在通话结束后恢复服务?,android,android-service,phone-state-listener,Android,Android Service,Phone State Listener,我的应用程序有一个简单的服务,当用户拨打或接听电话时,我需要暂停该服务,通话结束后服务必须恢复 以下是我的主要活动- public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main)

我的应用程序有一个简单的服务,当用户拨打或接听电话时,我需要暂停该服务,通话结束后服务必须恢复

以下是我的主要活动-

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent(getApplicationContext(), MyService.class);
    startService(intent);


    TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    TelephonyMgr.listen(new TeleListener(),
            PhoneStateListener.LISTEN_CALL_STATE);
}


class TeleListener extends PhoneStateListener {
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
                        Toast.LENGTH_LONG).show();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
                        Toast.LENGTH_LONG).show();
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                Toast.makeText(getApplicationContext(), incomingNumber,
                        Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
                        Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }
}}
这是我的服务类文件:-

public class MyService extends Service {
private static final String TAG = "MyService";

private boolean isRunning = false;

@Override
public void onCreate() {
    Log.i(TAG, "Service onCreate");

    isRunning = true;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Log.i(TAG, "Service onStartCommand");
    new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < Integer.MAX_VALUE; i++) {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                }

                if (isRunning) {
                    Log.i(TAG, "Service running");
                }
            }

            stopSelf();
        }
    }).start();

    return Service.START_STICKY;
}


@Override
public IBinder onBind(Intent arg0) {
    Log.i(TAG, "Service onBind");
    return null;
}

@Override
public void onDestroy() {

    isRunning = false;

    Log.i(TAG, "Service onDestroy");
}}
公共类MyService扩展服务{
私有静态最终字符串TAG=“MyService”;
私有布尔值isRunning=false;
@凌驾
public void onCreate(){
Log.i(标记“serviceoncreate”);
isRunning=true;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
Log.i(标签,“服务启动命令”);
新线程(newrunnable()){
@凌驾
公开募捐{
对于(int i=0;i

MainActivity中的TeleListener类,我能够获取电话状态。我的问题是,当TelephonyManager等于调用状态离线时,我需要暂停我的服务,当TelephonyManager等于调用状态空闲时,我需要恢复调用状态。你需要使用onCallStateChanged方法

将这行代码放入onCreate()方法中,它将初始化TelephonyManager的对象,并为您设置侦听器

TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
ListenToPhoneState listener = new ListenToPhoneState()
tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
innerclass ListenToPhoneState的类定义如下所示

private class ListenToPhoneState extends PhoneStateListener {

    boolean callEnded=false;
    public void onCallStateChanged(int state, String incomingNumber) {

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            UTILS.Log_e("State changed: " , state+"Idle");


            if(callEnded)
            {
                //you will be here at **STEP 4**
                //you should stop service again over here
            }
              else
              {
                //you will be here at **STEP 1**
             //stop your service over here,
                //i.e. stopService (new Intent(`your_context`,`CallService.class`));
                //NOTE: `your_context` with appropriate context and `CallService.class` with appropriate class name of your service which you want to stop.

              }


            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            UTILS.Log_e("State changed: " , state+"Offhook");
                //you will be here at **STEP 3**
             // you will be here when you cut call
            callEnded=true;
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            UTILS.Log_e("State changed: " , state+"Ringing");
                //you will be here at **STEP 2**

            break;


        default:
            break;
        }
    }

}
解释:在通话过程中,您的听众会经历以下状态:

步骤1:
TelephonyManager.CALL\u STATE\u IDLE

最初,您的调用状态将是空闲的,这就是为什么callEnded变量的值为false

第二步:
TelephonyManager.CALL\u STATE\u RINGING

现在,您正在等待对方接听您的电话

第3步:
TelephonyManager.CALL_STATE_OFFHOOK

你不接电话了

第4步:
TelephonyManager.CALL\u STATE\u IDLE

又闲着

注意:如果您不想知道调用何时结束以及结束调用后应该做什么,那么只要在
TelephonyManager.call\u STATE\u IDLE的块中输入时删除callEnded变量并停止服务即可


我希望这会有帮助

您需要使用onCallStateChanged方法

将这行代码放入onCreate()方法中,它将初始化TelephonyManager的对象,并为您设置侦听器

TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
ListenToPhoneState listener = new ListenToPhoneState()
tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
innerclass ListenToPhoneState的类定义如下所示

private class ListenToPhoneState extends PhoneStateListener {

    boolean callEnded=false;
    public void onCallStateChanged(int state, String incomingNumber) {

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            UTILS.Log_e("State changed: " , state+"Idle");


            if(callEnded)
            {
                //you will be here at **STEP 4**
                //you should stop service again over here
            }
              else
              {
                //you will be here at **STEP 1**
             //stop your service over here,
                //i.e. stopService (new Intent(`your_context`,`CallService.class`));
                //NOTE: `your_context` with appropriate context and `CallService.class` with appropriate class name of your service which you want to stop.

              }


            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            UTILS.Log_e("State changed: " , state+"Offhook");
                //you will be here at **STEP 3**
             // you will be here when you cut call
            callEnded=true;
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            UTILS.Log_e("State changed: " , state+"Ringing");
                //you will be here at **STEP 2**

            break;


        default:
            break;
        }
    }

}
解释:在通话过程中,您的听众会经历以下状态:

步骤1:
TelephonyManager.CALL\u STATE\u IDLE

最初,您的调用状态将是空闲的,这就是为什么callEnded变量的值为false

第二步:
TelephonyManager.CALL\u STATE\u RINGING

现在,您正在等待对方接听您的电话

第3步:
TelephonyManager.CALL_STATE_OFFHOOK

你不接电话了

第4步:
TelephonyManager.CALL\u STATE\u IDLE

又闲着

注意:如果您不想知道调用何时结束以及结束调用后应该做什么,那么只要在
TelephonyManager.call\u STATE\u IDLE的块中输入时删除callEnded变量并停止服务即可


我希望这会有帮助

您不能暂停或恢复服务,您可以在通话结束时重新启动服务。在
PhoneStateListener
TelephonyManager中。调用\u STATE\u OFFHOOK
常量,再次启动您的服务!使用@Tijo提到的
PhoneStateListener
的不同状态,您可以决定何时启动或停止服务。

您不能暂停或恢复服务,您可以在通话结束时再次启动服务。在
PhoneStateListener
TelephonyManager中。调用\u STATE\u OFFHOOK
常量,再次启动您的服务!使用@Tijo提到的
PhoneStateListener
的不同状态,您可以决定何时启动或停止服务。

我已经声明了所有这些。我需要暂停并恢复CALL_STATE_OFFHOOK和CALL_STATE_Idle上的服务。它工作正常,但它只在应用程序打开时工作,但我需要像服务一样一直工作。我已经声明了所有这些。我需要暂停并恢复CALL_STATE_OFFHOOK和CALL_STATE_Idle上的服务。它工作正常,但它只在应用程序打开时工作,但我需要像服务一样一直工作。