【问题标题】:Incoming number during a call in android?在android通话期间来电?
【发布时间】:2023-04-04 04:45:01
【问题描述】:

我正在运行以下代码,它正在编译,但我没有得到任何结果或 toast 显示请帮助...

CustomBroadcastReceiver.java 此类将接收动作电话状态更改并将实例化 customephonestatelistener

public class CustomBroadcastReceiver extends BroadcastReceiver {

TelephonyManager telephony = 
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener(context);

telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);


Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
    Log.v(TAG, "phoneNr: "+phoneNr);
    Toast toast = Toast.makeText(context, "phoneNr: "+phoneNr, Toast.LENGTH_LONG);
    toast.show();
}
}

CustomPhonestateListener.java

这个类是主要的操作类

public class CustomPhoneStateListener extends PhoneStateListener {

private static final String TAG = "CustomPhoneStateListener";

private Context mContext;

public CustomPhoneStateListener(Context context) {


// TODO Auto-generated constructor stub
mContext = context;
}

public void onCallStateChanged(int state, String incomingNumber){

    Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");

     Log.v(TAG, incomingNumber);
    Toast toast = Toast.makeText(mContext, "WE ARE INSIDE!!!!!!!!!!!", Toast.LENGTH_LONG);
    toast.show();

    switch(state){
            case TelephonyManager.CALL_STATE_RINGING:
                    Log.d(TAG, "RINGING");
                    break;
    }       

AndroidManifest.xml

  <uses-permission android:name="android.permission.RECEIVE_SMS" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WRITE_CONTACTS" />
  <uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name=".MyBroadcastReceiver">
            <intent-filter>
                    <action android:name="android.intent.action.PHONE_STATE" />

            </intent-filter>
    </receiver>
</application>

【问题讨论】:

  • 请上传你的代码的logcat
  • 请修改CustomBroadcastReceiver的正确代码,这里显示的java无效

标签: android broadcastreceiver call telephony phone-state-listener


【解决方案1】:

谢谢答案...我尝试了另一种方法,以下代码似乎有效..加入了接收器和 phonestatelistener..但现在的问题是我必须重新启动设备才能启动此服务..有什么建议我现在错了吗?

public class IncomingCallReciever extends BroadcastReceiver {

private Context mContext;
private Intent mIntent;

@Override
public void onReceive(Context context, Intent intent) {
    mContext = context;
    mIntent = intent;
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    int events = PhoneStateListener.LISTEN_CALL_STATE;
    tm.listen(phoneStateListener, events);
}

private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        String callState = "UNKNOWN";
        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            callState = "IDLE";
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            // -- check international call or not.
            if (incomingNumber.startsWith("00")) {
                Toast.makeText(mContext,"International Call- " + incomingNumber,Toast.LENGTH_LONG).show();
                callState = "International - Ringing (" + incomingNumber+ ")";
            } else {
                Toast.makeText(mContext, "Local Call - " + incomingNumber, Toast.LENGTH_LONG).show();
                callState = "Local - Ringing (" + incomingNumber + ")";
            }
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            String dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            if (dialingNumber.startsWith("00")) {
                Toast.makeText(mContext,"International - " + dialingNumber,Toast.LENGTH_LONG).show();
                callState = "International - Dialing (" + dialingNumber+ ")";
            } else {
                Toast.makeText(mContext, "Local Call - " + dialingNumber,Toast.LENGTH_LONG).show();
                callState = "Local - Dialing (" + dialingNumber + ")";
            }
            break;
        }
        Log.i(">>>Broadcast", "onCallStateChanged " + callState);
        super.onCallStateChanged(state, incomingNumber);
    }
};

}

【讨论】:

    【解决方案2】:

    问题出在你的清单文件中,你必须有这个:

    <receiver android:name=".MyReceiver" >
    <intent-filter>
    <action android:name="android.intent.action.PHONE_STATE" />
    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />                
    </intent-filter>
    </receiver>
    

    而不是:

    <receiver android:name=".MyReceiver" >
    <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />                
    </intent-filter>
    </receiver>
    

    【讨论】:

      【解决方案3】:

      “但现在的问题是我必须重新启动设备才能启动此服务。有什么建议我现在错了吗?”

      您必须重新启动手机的原因是在您的 AndroidManifest.xml 文件中您只允许执行一项操作...

      <!-- Your Problem is here -->
      <action android:name="android.intent.action.BOOT_COMPLETED" />
      

      您已将 BroadcastReceiver 设置为仅在手机启动时启动。
      要解决此问题,您需要使用 DR VOLT 提供的答案。

      只需将操作 PHONE_STATE 和 NEW_OUTGOING_CALL 添加到您的接收器
      例如:

      <receiver android:name=".MyReceiver" >
      <intent-filter>
      <action android:name="android.intent.action.PHONE_STATE" />
      <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
      <action android:name="android.intent.action.BOOT_COMPLETED" />           
      </intent-filter>
      </receiver>
      

      这将在这些操作发生时启动您的广播接收器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-03
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多