听换手机州州 - Android电子 [英] Listen to Change of Phone State State - Android

查看:223
本文介绍了听换手机州州 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在试图创建一个简单的应用程序,记录你已经花了多少分钟通话,然后提醒你,当你接近你的免费通话时间。

在这一点上,我创建了一个名为CallService.java服务,只要用户来电人们被调用。该服务仅记录了,而呼叫的结束时间的开始时间。该服务使用的是被称为OutgoingCallReciever.Java类开始。这个类简单地等待用户呼叫某人,然后开始CallService。

我现在正在试图阻止CallService当用户手机没有通话的人。即(电话状态是空闲,摘机,或其他人呼吁用户),但我不知道该怎么做(我是新来的Java / Android的)。难道我用PhoneStateListener的onCallStateChanged方法? (我不能确定如何使用它。)

希望能帮到你!

的类是如下:

MainActivity.java

 包com.fouadalnoor.callcounter;进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Intent;
进口android.view.Menu;
进口android.widget.CheckBox;
进口android.widget.CompoundButton;
进口android.widget.Toast;
进口android.widget.CompoundButton.OnCheckedChangeListener;
进口android.telephony.TelephonyManager;
进口android.telephony.PhoneStateListener;公共类MainActivity延伸活动{     @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        TelephonyManager TM =(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        PhoneStateListener PS =(PhoneStateListener)getSystemService(TELEPHONY_SERVICE);         Toast.makeText(这一点,手机状态=+ tm.getCallState(),Toast.LENGTH_LONG).show();
        复选框复选框=(复选框)findViewById(R.id.checkBox1);
        checkBox.setOnCheckedChangeListener(新OnCheckedChangeListener(){                @覆盖
                公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){                    如果(器isChecked){
                        stopService(新意图(buttonView.getContext(),CallService.class));
                    }
                }
            });    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }}

OutgoingCallReciever.java

 包com.fouadalnoor.callcounter;进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
公共类OutgoingCallReciever扩展广播接收器{
     @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
          context.startService(新意图(上下文,CallService.class));
     }}

CallService.java

 包com.fouadalnoor.callcounter;进口android.app.Service;
进口android.content.Context;
进口android.content.Intent;
进口android.os.IBinder;
进口android.widget.Toast;
进口java.lang.String中;
公共类CallService延伸服务{    众长的startTime,结束时间,TOTALTIME;    @覆盖
    公众的IBinder onBind(意向意图){
        // TODO自动生成方法存根
        返回null;
    }        @覆盖
        公共无效的onDestroy(){            Toast.makeText(这一点,呼叫服务已停止,Toast.LENGTH_LONG).show();            ENDTIME = System.currentTimeMillis的()/ 1000;
            Toast.makeText(这一点,ENDTIME =+结束时间,Toast.LENGTH_LONG).show();
            TOTALTIME = ENDTIME-的startTime;
            Toast.makeText(这一点,TOTALTIME =+ TOTALTIME,Toast.LENGTH_LONG).show();        }        @覆盖
        公共无效调用onStart(意向意图,诠释startid){
            Toast.makeText(这一点,呼叫服务已启动用户,Toast.LENGTH_LONG).show();            STARTTIME = System.currentTimeMillis的()/ 1000;
            Toast.makeText(这一点,STARTTIME =+的startTime,Toast.LENGTH_LONG).show();
        }
}


解决方案

是的,你需要使用 onCallStateChanged 方法。

把这个线在你的的onCreate()方法,它将初始化 TelephonyManager 的对象,将设置监听器你。

  TelephonyManager tManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
ListenToPhoneState监听器=新ListenToPhoneState()
tManager.listen(监听器,PhoneStateListener.LISTEN_CALL_STATE);

内部类的类定义的 ListenToPhoneState 将寻找这样的,

 私有类ListenToPhoneState扩展PhoneStateListener {        布尔callEnded = FALSE;
        公共无效onCallStateChanged(INT状态,弦乐incomingNumber){            开关(州){
            案例TelephonyManager.CALL_STATE_IDLE:
                UTILS.Log_e(国家改变:国家+空闲);
                如果(callEnded)
                {
                    //你将在这里**第4步**
                    //你应该在这里再次停止服务
                }
                  其他
                  {
                    //你将在这里** STEP 1 **
                 //在这里停止服务,
                    //i.e。 stopService(新意图(`your_context`,`CallService.class`));
                    //注意:`your_context`适当的环境和`与您要停止服务的适当的类名CallService.class`。                  }
                打破;
            案例TelephonyManager.CALL_STATE_OFFHOOK:
                UTILS.Log_e(国家改变:国家+摘机);
                    //你将在这里**第3步**
                 //你将出现在这里你砍电话
                callEnded = TRUE;
                打破;
            案例TelephonyManager.CALL_STATE_RINGING:
                UTILS.Log_e(国家改变:国家+振铃);
                    //你将在这里**第2步**                打破;
            默认:
                打破;
            }
        }    }

释解:
虽然电话,侦听器将通过以下状态,

第1步 TelephonyManager.CALL_STATE_IDLE

最初的呼叫状态将被闲置,这就是为什么变量 callEnded 将具有值

第2步 TelephonyManager.CALL_STATE_RINGING

现在您正在等待接收人收到您的电话

第3步 TelephonyManager.CALL_STATE_OFFHOOK

您切断电话

第四步 TelephonyManager.CALL_STATE_IDLE

再次空闲

注意:如果您不想知道当通话结束,什么应该结束通话,然后只是删除后进行 callEnded 变量,只要您在 TelephonyManager.CALL_STATE_IDLE

块进入停止服务

我希望这会有所帮助!

I am currently trying to create a simple app that records how many minutes you have spent calling and then warn you when you are close to your free minutes.

At this point I have created a service called CallService.java that is called whenever the user calls people. This service simply records the start time of the call and the end time of the call. The service is started using a class called OutgoingCallReciever.Java. This class simply waits for the user to call someone and then starts the CallService.

I am now trying to stop the CallService when the users phone is not calling someone. i.e (phone state is idle, off-hook, or someone else is calling the user) but I dont know how to do that (I am new to Java/Android). Do I use PhoneStateListener 's onCallStateChanged method? (I am unsure how to use it..)

Hope you can help!

The classes are below:

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


}

解决方案

Yes, you need to use onCallStateChanged method.

put this lines in your onCreate() method,it will initialize object of TelephonyManager and will setup listener for you.

TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
ListenToPhoneState listener = new ListenToPhoneState()
tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

class definition of innerclass ListenToPhoneState will be looking like this,

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

    }

Explaination: While the call,your listener will go through following states,

Step 1: TelephonyManager.CALL_STATE_IDLE

initially your call state will be idle that is why the variable callEnded will have the value false.

Step 2: TelephonyManager.CALL_STATE_RINGING

now you are waiting for the receiver person to receive your call

Step 3: TelephonyManager.CALL_STATE_OFFHOOK

you cut the call

Step 4: TelephonyManager.CALL_STATE_IDLE

idle again

NOTE: If you don't want to know when the call ends and what should be done after ending the call then just remove callEnded variable and stop the service whenever you enter in the block of TelephonyManager.CALL_STATE_IDLE

I hope it will be helpful !!

这篇关于听换手机州州 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆