- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在尝试创建一个简单的应用程序来记录您通话的分钟数,然后在您接近空闲时间时提醒您。
此时,我已经创建了一个名为 CallService.java 的服务,每当用户调用他人时都会调用该服务。该服务只是记录通话的开始时间和通话的结束时间。该服务使用名为 OutgoingCallReciever.Java 的类启动。此类只是等待用户调用某人,然后启动 CallService。
我现在正试图在用户电话未调用某人时停止 CallService。即(电话状态为空闲、摘机或其他人正在调用用户)但我不知道该怎么做(我是 Java/Android 的新手)。我是否使用 PhoneStateListener 的 onCallStateChanged 方法? (我不确定如何使用它..)
希望对您有所帮助!
类如下:
主 Activity .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));
}
}
调用服务.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 - 监听电话状态的变化 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14537403/
我有一个包含姓名、电子邮件和内容文本区域的表单。一切正常,但当我尝试为手机号码添加 1 个表单输入时,表单不会提交。 Javascript: function hgsubmit() { i
所以我正试图在 Scheme 中找出整个 call/cc 的东西。下面是我正在使用的代码: (+ 1 (call/cc (lambda (k) (if (number? k)
所以我正试图在 Scheme 中找出整个 call/cc 的东西。下面是我正在使用的代码: (+ 1 (call/cc (lambda (k) (if (number? k)
我在 list 中有权限: 检查电话是否正在使用的代码可能会为无法接听电话的平板电脑等设备启动安全异常。所以,我制作了这个方法来检查是否设备可以使用 TelephonyManager: priva
我想知道 facebook API 允许 PHP 网页提取以下用户数据的先决条件是什么: 姓名 电子邮件 电话 据我了解,提取电话号码需要您的网站在 facebook/Websense 的白名单中吗?
我如何从我的应用程序调用特定号码的电话?给我一些执行此任务的逻辑或代码... 最佳答案 使用 UIApplication 的 openURL: 方法: [[UIApplication sharedAp
在 URI 中,空格可以编码为 + .既然如此,那么在创建具有国际前缀的 tel URI 时是否应该对前导加号进行编码? 哪个更好?两者在实践中都有效吗? Call me Call me 最佳答案 不
我正在尝试使用 Insomnia 调用 SOAP 电话 - 特别是试图让帖子成功。我将 URL 定义为端点,并将正文类型作为带有 SOAP 内容(信封、标题、正文)的 XML。我在标题中定义了用户 I
服务器有没有办法将一些数据无线无缝地推送到客户端,可能是 Windows(电话)、iPhone、Mac 或 Android 设备,没有任何操作系统集成? 如果是这样,最好的设计模式是什么,最好的技术是
我有一个搜索字段,我希望用户能够通过电话或电子邮件进行搜索。但是,我的 onclick 事件没有触发。我已经尝试过控制台工具,但似乎无法通过那里进行调试。 {% from "_form
如果重新加载接收调用的浏览器窗口,我将面临实时 Twilio 调用被丢弃的情况。有没有办法在不影响实时通话的情况下克服这种挫折? 最佳答案 Twilio 布道者在这里。 根据您的问题,我假设您使用的是
我试图在我的服务中减少我的 promise 对象。我有类似的东西 angular.module('myApp').service('testService', ['Employees','$q',
我想在我的java应用程序中构建一个电话调用器。为此,我使用了 JAIN-SIP 库。第一次 INVITE 后,系统需要代理身份验证。第二个邀请是在“AuthenticationHelperImpl.
两天前我正在开发一个 VOIP 应用程序,并成功地使用 Sinch 实现了一个普通的应用程序到应用程序调用。 .该应用程序运行良好。当我开始时,我在他们的文档中看到他们支持电话 session 。 现
我正在尝试创建一个 java 程序,它将创建 excel 文件并将其上传到谷歌驱动器中。上传后我需要它来授予权限。我已经完成了所有这些,但问题在于尝试将 excel 文件转换为 google 文件,以
我想使用 Android Sip 进行电话 session .那可能吗?有人可以举一个有效的例子吗?使用该库是否有任何限制,比如它可以在 3G 或 4G 上工作? 最佳答案 当前的 Android S
是否可以通过编程方式在 android 中接听电话? 我发现有些地方不可能,但后来安装了应用程序 https://play.google.com/store/apps/details?id=com.a
我试图在单击导航中的某个按钮后将一个类附加到 div。 $navButtons.on('click', navigationClick); // liseten to the navigation a
在 Monotouch 中是否有任何方法可以读取电话或调用者服务的当前状态? 我正在尝试寻找某种方式来读取通话是否处于事件状态或通话是否处于等待状态等。 用谷歌搜索没有找到任何结果。 希望运行一些代码
我可能正在搜索错误的内容,这可能解释了为什么我只能找到有关设置密码自动填充的信息。 我正在寻找用户能够在应用程序中输入电子邮件、电话、名字和姓氏的功能,就像某些网站能够做到的那样,选项出现在键盘上方。
我是一名优秀的程序员,十分优秀!