【问题标题】:Receiving call intent in broadcast receiver在广播接收器中接收呼叫意图
【发布时间】:2017-02-22 13:31:30
【问题描述】:

我想在 android 中获取被叫号码,但是当我开始拨出电话时它失败了

Menifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vampirepc.androidservice" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.BOOT_COMPLETED">
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />


            </action>
        </intent-filter>
    </activity>

    <service android:name=".MyService" />


    <receiver
        android:name="com.example.vampirepc.androidservice.OutgoingReceiver"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

</application>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
</manifest>

BroadcastReceiver 类

  @Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(ctx,
            "Inside broadcast",
            Toast.LENGTH_LONG).show();


    String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

    Toast.makeText(context, "Outgoing call catched: " + phoneNumber, Toast.LENGTH_LONG).show();


}

服务

  @Override
public void onCreate() {
    Toast.makeText(getApplicationContext(),"Service created",Toast.LENGTH_SHORT).show();

    try {


        IntentFilter filter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL");

        OutgoingReceiver myReceiver = new OutgoingReceiver();
        registerReceiver(myReceiver, filter);


    } catch (Exception e) {

        Toast.makeText(getApplicationContext(),"Exception is "+String.valueOf(e),Toast.LENGTH_SHORT).show();

    }



}

【问题讨论】:

  • 在您的情况下,无需在运行时注册接收器,只需在清单中定义即可。
  • 当我拨打号码时仍然无法正常工作应用程序停止工作
  • 请发布堆栈跟踪。
  • 当我删除 onReceive 方法中的所有代码时,它不会停止应用程序。
  • 我认为问题出在 Toast 上。

标签: java android android-intent broadcastreceiver


【解决方案1】:

【讨论】:

    【解决方案2】:

    请在顶部添加权限,这对我有帮助

          ?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.vampirepc.androidservice" >
    
        android:name="android.permission.PROCESS_OUTGOING_CALLS" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme" >
            <activity android:name=".MainActivity" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                    <action android:name="android.intent.action.BOOT_COMPLETED">
                        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    
    
                    </action>
                </intent-filter>
            </activity>
            <receiver
                android:name="com.example.vampirepc.androidservice.OutgoingReceiver"
                android:enabled="true"
                android:exported="true" >
                <intent-filter>
                    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
                </intent-filter>
            </receiver>
    
      <service android:name=".MyService" />
        </application>
        <uses-permission 
        </manifest>
    

    【讨论】:

    【解决方案3】:

    你有没有意识到这一行:

    Toast.makeText(ctx,
            "Inside broadcast",
            Toast.LENGTH_LONG).show();
    

    你使用了 ctx 而不是 context。

    【讨论】:

      【解决方案4】:

      感谢所有我解决了我自己的问题,我在烤面包机中使用了不正确的上下文,正确的 onReceive 是。 我使用的是初始化的 ctx。

      Context ctx;
      
      
         public void onReceive(Context context, Intent intent) {
      
          Toast.makeText(context, "this is not shown"     , Toast.LENGTH_LONG).show();
      
      
      
          String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
      
          Toast.makeText(context, "Outgoing call catched: " + phoneNumber, Toast.LENGTH_LONG).show();
      
      
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多