簡體   English   中英

在廣播接收器中接收呼叫意圖

[英]Receiving call intent in broadcast receiver

我想在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();

    }



}

請在頂部添加權限,這對我有幫助

      ?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>

您是否意識到這一點:

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

您使用ctx而不是上下文。

感謝所有我已經解決的問題,我自己在烤面包機中使用了錯誤的上下文,正確的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();


 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM