【问题标题】:Do something when a phone call is received接到电话时做某事
【发布时间】:2014-05-26 23:09:03
【问题描述】:

我正在尝试创建一个应用程序,该应用程序将在收到呼叫时执行不同的功能。为了做一个小的工作示例,我让我的课程扩展 BroadcastReceiver 并且我试图让 toast 通知显示出来。

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

public class IncomingCallInterceptor extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Do something.", Toast.LENGTH_LONG).show();
    }
}

我已在我的AndroidManifest.xml 文件中添加了此权限:

<application android:icon="@drawable/icon" android:label="Incoming Call Interceptor">
    <receiver android:name="IncomingCallInterceptor">
        <intent-filter>
             <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
    </receiver>
</application>

我的测试设备运行的是 Android 4.4.2。当有人来电时,不会出现 Toast 通知。

【问题讨论】:

  • 您是否在清单中注册了接收方?
  • 我已经编辑了原始帖子以包含我的AndroidManifest.xml 中定义的接收者。也许我的声明方式有问题?

标签: android broadcastreceiver android-toast


【解决方案1】:

试试这个代码到Monitor the state of the Phone

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class PhoneReceiver extends PhoneStateListener {
Context context;
public PhoneReceiver(Context context) {
this.context = context;
}
@Override
public void onCallStateChanged(int state, 
String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
Toast.makeText(context, "onCallStateChanged state=" + 
state + "incomingNumber=" + incomingNumber, 
Toast.LENGTH_LONG).show(); 
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(context, "idle", 
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(context, "ringing", 
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(context, "offhook", 
Toast.LENGTH_LONG).show();
break;
}
}
}

【讨论】:

    【解决方案2】:

    从类似的帖子中找到答案:Incoming call broadcast receiver not working (Android 4.1)

    您必须在 广播接收器从 Android 3.0 开始工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-19
      相关资源
      最近更新 更多