如何获得只有断开电话后警报对话框? [英] How to get alert dialog only after disconnect call?

查看:141
本文介绍了如何获得只有断开电话后警报对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我在做什么,当呼叫被呼叫方或接收器断开,一个警报对话框应与手机number.it出现一件事情都工作正常,但问题是,当警觉我得到的呼叫对话也显示,但我只想只有断开后,我不知道什么是错误,我想提出以下是我的code..can任何一个帮助吗?

 公共类MyCallReceiver扩展广播接收器{私人字符串incomingNumber;@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    // TODO自动生成方法存根
    如果(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)){
        //当手机有来电此code将执行        //获取电话号码        incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        意图I =新意图(背景下,Disp_Alert_dialog.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra(号码,incomingNumber);
        context.startActivity(ⅰ);
        Toast.makeText(背景,从电话:+ incomingNumber,Toast.LENGTH_LONG).show();
       / *字符串incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Toast.makeText(背景,从电话:+ incomingNumber,Toast.LENGTH_LONG).show(); * /    }否则如果(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
            TelephonyManager.EXTRA_STATE_IDLE)
            || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                    TelephonyManager.EXTRA_STATE_OFFHOOK)){
        //这个code将执行通话时断开    }
}


解决方案

您的条件是错误的。您正在使用的else块

 如果(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)

它将运行时调用状态更改为理想或摘机(在拨出电话 - 拨打和呼叫断开,在来电的时候 - 当呼叫被接听和呼叫断开)。

如果你想显示的对话只有在切断通话,使用

 如果(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDEAL)

在写code if块

至于号码,你不能这样,因为它仅适用于振铃状态号码。
一个解决将使用<一个href=\"http://developer.android.com/reference/android/database/ContentObserver.html%22ContentObserver%22\"相对=nofollow> ContentObserver 上CallLog。它的的onChange()每当有通话记录的改变将被调用。然后,您可以采取一些从通话记录。但是,你需要有积极为这个应用程序的一部分。使用在服务的可能。

注意 - 在某些设备消息还来自于呼叫记录中的数据。所以,检查是否在通话记录中的最后一个条目的呼叫类型或不显示你的对话之前,

<一个href=\"http://stackoverflow.com/questions/4422410/how-to-implement-a-contentobserver-for-call-logs\">This可以帮助你在使用内容观察者

In my app i am doing one thing that,when the call is disconnected by caller or receiver,one alert dialog should appear with cellphone number.it all works fine but issue is alert dialog is also appearing when i am getting call,but i only want only after disconnection,i don't know what is mistake i am making following is my code..can any one help?

     public class MyCallReceiver extends BroadcastReceiver {

private String incomingNumber;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    if  (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)) {
        // This code will execute when the phone has an incoming call

        // get the phone number 

        incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        Intent i = new Intent(context, Disp_Alert_dialog.class); 
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("Number", incomingNumber);
        context.startActivity(i);
        Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();


       /* String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();*/

    } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
            TelephonyManager.EXTRA_STATE_IDLE)
            || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                    TelephonyManager.EXTRA_STATE_OFFHOOK)) {
        // This code will execute when the call is disconnected



    }
}

解决方案

You condition is wrong. You are using else block of

if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)

It will run whenever call state changes to 'Ideal' or 'off hook' (in outgoing call - when dialed and call is disconnected, in incoming call - when call is picked up and call is disconnected).

If you want to show dialogue only while disconnecting call, use

if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDEAL)

and write code in if block

As for number, you cannot get number that way as it is only valid for ringing state. A work around would be using a ContentObserver on CallLog. Its onChange() will be called whenever there is a change in call log. You can then take number from call log. But you will need to have a part of your application active for this. Use in a Service may be.

Note- In some devices message also comes in call log data. So check if last entry in call log in of call type or not before showing your dialogue

This may help you in using content observer

这篇关于如何获得只有断开电话后警报对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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