I am working on fetching recent call log as call get disconnected(outgoing , incoming) either answered or unanswered.
我正在努力获取最近的呼叫日志,因为呼叫断开(传出,传入)应答或未应答。
I am using Phone state listener to fire broadcast when call get disconnected but it getting fired multiple time for one call why so..??
我正在使用Phone状态监听器来解决当呼叫断开连接时的广播,但是为了一次呼叫,它被多次激活,为什么呢?
So Please tell me how to fire receiver only once for one call.
所以请告诉我如何只接听一次电话接收器。
here is my code
这是我的代码
public class BroadcastReceiver extends android.content.BroadcastReceiver{
static boolean iscallended= true;
Context mContext;
TelephonyManager telephony;
private static final String TAG = "CustomBroadcastReceiver";
CustomPhoneStateListener customPhoneStateListener;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
mContext = context;
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
telephony = (TelephonyManager)context.getSystemService(context.TELEPHONY_SERVICE);
if(customPhoneStateListener==null)
{
customPhoneStateListener = new CustomPhoneStateListener();
telephony.listen(customPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
}
private class CustomPhoneStateListener extends PhoneStateListener
{
private static final String TAG = "CustomPhoneStateListener";
Handler handler=new Handler();
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
System.out.println(iscallended+ " value of iscancelled ");
switch (state)
{
case TelephonyManager.CALL_STATE_RINGING:
if(!incomingNumber.equalsIgnoreCase(""))
{
//YOUR CODE HERE
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if(iscallended)
{
iscallended = false;
System.out.println("IDLE called");
Toast.makeText(mContext, "IDLE", Toast.LENGTH_SHORT).show();
Intent it = new Intent(mContext,MainActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(it);
}
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
telephony.listen(customPhoneStateListener, PhoneStateListener.LISTEN_NONE);
}
}
}
Here's receiver in manifest
这是清单中的接收者
<receiver android:name="com.example.calllogs.BroadcastReceiver">
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
2 个解决方案
#1
0
Here is your answer :
这是你的答案:
https://*.com/a/5497316/3479012
also specify permission in your manifest to access phone state.
还可以在清单中指定访问手机状态的权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
also have a look at this http://karanbalkar.com/2014/02/detect-incoming-call-and-call-hangup-event-in-android/
也看看这个http://karanbalkar.com/2014/02/detect-incoming-call-and-call-hangup-event-in-android/
#2
0
Yes you will get that.
是的,你会得到的。
private class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
//Hangup
case TelephonyManager.CALL_STATE_IDLE:
Log.d("IDLE", "Call Idle" + state);
if (isCallEnded) {
isCallEnded = false;
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
.setCancelable(false)
.setPositiveButton(getString(R.string.Yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// your method
}
})
.setNegativeButton(getString(R.string.No), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
alertBuilder.show();
}
break;
//Outgoing
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("OFFHOOK", "Call OffHook" + state);
isCallEnded = true;
break;
//Incoming
case TelephonyManager.CALL_STATE_RINGING:
Log.d("RINGING", "Call Ringing" + state);
break;
}
}
}
The above is sample, when you look at the device log you'll definitely see offhook multiple times as well as IDLE state.
以上是示例,当您查看设备日志时,您肯定会看到多次摘机以及IDLE状态。
Try to calculate that, it should be okay.
试着计算一下,它应该没问题。
#1
0
Here is your answer :
这是你的答案:
https://*.com/a/5497316/3479012
also specify permission in your manifest to access phone state.
还可以在清单中指定访问手机状态的权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
also have a look at this http://karanbalkar.com/2014/02/detect-incoming-call-and-call-hangup-event-in-android/
也看看这个http://karanbalkar.com/2014/02/detect-incoming-call-and-call-hangup-event-in-android/
#2
0
Yes you will get that.
是的,你会得到的。
private class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
//Hangup
case TelephonyManager.CALL_STATE_IDLE:
Log.d("IDLE", "Call Idle" + state);
if (isCallEnded) {
isCallEnded = false;
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
.setCancelable(false)
.setPositiveButton(getString(R.string.Yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// your method
}
})
.setNegativeButton(getString(R.string.No), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
alertBuilder.show();
}
break;
//Outgoing
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("OFFHOOK", "Call OffHook" + state);
isCallEnded = true;
break;
//Incoming
case TelephonyManager.CALL_STATE_RINGING:
Log.d("RINGING", "Call Ringing" + state);
break;
}
}
}
The above is sample, when you look at the device log you'll definitely see offhook multiple times as well as IDLE state.
以上是示例,当您查看设备日志时,您肯定会看到多次摘机以及IDLE状态。
Try to calculate that, it should be okay.
试着计算一下,它应该没问题。