听取手机状态的变化 – Android

我目前正在尝试创建一个简单的应用程序,记录您拨打电话的时间,然后在接近您的免费分钟时警告您.

此时,我创建了一个名为CallService.java的服务,只要用户调用人员就会调用该服务.该服务仅记录呼叫的开始时间和呼叫的结束时间.该服务使用名为OutgoingCallReciever.Java的类启动.该类只是等待用户呼叫某人然后启动CallService.

我正在尝试在用户电话没有呼叫某人时停止CallService.即(手机状态闲置,摘机或其他人正在呼叫用户)但我不知道该怎么做(我是Java / Android的新手).我是否使用PhonestateListener的onCallStateChanged方法? (我不确定如何使用它..)

希望你能帮忙!

课程如下:

MainActivity.java

package com.fouadalnoor.callcounter;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedchangelistener;
import android.telephony.TelephonyManager;
import android.telephony.PhonestateListener;

public class MainActivity extends Activity {

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

        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        PhonestateListener ps =(PhonestateListener) getSystemService(TELEPHONY_SERVICE);

         Toast.makeText(this,"Phone State = " + tm.getCallState(),Toast.LENGTH_LONG).show();


        CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
        checkBox.setonCheckedchangelistener(new OnCheckedchangelistener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

                    if(isChecked){
                        stopService (new Intent(buttonView.getContext(),CallService.class));
                    }
                }
            });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main,menu);
        return true;
    }

}

OutgoingCallReciever.java

package com.fouadalnoor.callcounter;

import android.content.broadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class OutgoingCallReciever extends broadcastReceiver {


     @Override
        public void onReceive(Context context,Intent intent) {
          context.startService(new Intent(context,CallService.class));
     }

}

CallService.java

package com.fouadalnoor.callcounter;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import java.lang.String;


public class CallService extends Service {

    public long startTime,endTime,totalTime;

    @Override
    public IBinder onBind(Intent intent) {
        // Todo Auto-generated method stub
        return null;
    }

        @Override
        public void onDestroy() {

            Toast.makeText(this,"Call Service stopped",Toast.LENGTH_LONG).show(); 

            endTime = System.currentTimeMillis()/1000;
            Toast.makeText(this,"endTime = " + endTime,Toast.LENGTH_LONG).show();
            totalTime =  endTime-startTime; 
            Toast.makeText(this,"totalTime = " + totalTime,Toast.LENGTH_LONG).show();

        }

        @Override
        public void onStart(Intent intent,int startid) {
            Toast.makeText(this,"Call Service started by user.",Toast.LENGTH_LONG).show();

            startTime = System.currentTimeMillis()/1000;
            Toast.makeText(this,"startTime = "+ startTime,Toast.LENGTH_LONG).show();
        }


}

解决方法

是的,您需要使用onCallStateChanged方法.

将这些行放在你的onCreate()方法中,它将初始化TelephonyManager的对象并为你设置监听器.

TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
ListenToPhonestate listener = new ListenToPhonestate()
tManager.listen(listener,PhonestateListener.LISTEN_CALL_STATE);

内部类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_STATE_IDLE

最初你的调用状态将是空闲的,这就是变量callEnded将具有值false的原因.

第2步:TelephonyManager.CALL_STATE_RINGING

现在你正在等待接收者接听你的电话

第3步:TelephonyManager.CALL_STATE_OFFHOOK

你切断了电话

第4步:TelephonyManager.CALL_STATE_IDLE

再次闲置

注意:如果您不想知道呼叫何时结束以及结束呼叫后应该做什么,则只需删除callEnded变量并在您输入TelephonyManager.CALL_STATE_IDLE块时停止服务

我希望它会有所帮助!!

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...