电话响铃时的Toast通知

时间:2014-04-12 18:47:35

标签: android telephonymanager android-toast

我在这里关注这个例子(http://androidlabs.org/short-experiments/broadcast-receivers/do-something-when-the-phone-rings/):

package nl.codestone.cookbook.incomingcallinterceptor;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class IncomingCallInterceptor extends BroadcastReceiver {  // 1

@Override
public void onReceive(Context context, Intent intent) {           // 2
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);    // 3
    String msg = "Phone state changed to " + state;

    if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {              // 4
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);  // 5
        msg += ". Incoming number is " + incomingNumber;

        // TODO This would be a good place to "Do something when the phone rings" <img src="http://androidlabs.org/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley"> 

    }

    Toast.makeText(context, msg, Toast.LENGTH_LONG).show();

}

}

我已将项目直接导入Eclipse并将其安装在我的设备上。所有内容都正确编译和安装,没有错误消息,但是当我收到来电时似乎没有生成Toast消息。

我还试图手动创建项目,以防在导入过程中出现问题,但结果是一样的。即使它已安装,但在手机响铃时似乎没有做任何事情。

1 个答案:

答案 0 :(得分:0)

试试这个:

if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) { 
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
msg += ". Incoming number is " + incomingNumber;
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}

基本上,我正试图在

中做吐司
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state))