簡體   English   中英

廣播接收器未收到意圖

[英]Intent not received by Broadcast Receiver

我正在嘗試在用戶使用標准android撥號器撥打電話時顯示一條消息。

我的活動中包含以下代碼

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);         
        setTestButtonListener();
        Log.i(LOG_TAG, "Activity started...");

    }

    private void setTestButtonListener()
    {
    Button testButton = (Button)this.findViewById(R.id.testButton);
    testButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) 
        {

            Log.i(LOG_TAG, "Clicked on test...");
            Toast.makeText(getApplicationContext(), (CharSequence)"You clicked on the test button" ,Toast.LENGTH_SHORT).show();
             Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+150));
             MainActivity.this.sendOrderedBroadcast(intent, null);
             MainActivity.this.startActivity(intent);
             Log.i(LOG_TAG, "intent broadcasted... I think... ");
        }
     });
     }

然后在BroadcastReceiver中(從中派生一個類):

public class OnMakingCallReceiver extends BroadcastReceiver
{
    private static final String LOG_TAG = OnMakingCallReceiver.class.getSimpleName() + "_LOG";

    @Override
    public void onReceive(Context context,Intent intent)
    {   
            Log.i(LOG_TAG, " got here!");       
    }
}

然后在AndroidManifest.xml中

    <uses-permission android:name="android.permission.CALL_PHONE" />

    <receiver android:name=".OnMakingCallReceiver" android:priority="999">
        <intent-filter>
            <action android:name="android.intent.action.CALL"/>
        </intent-filter>
    </receiver>

我單擊測試按鈕,然后看到此輸出。

點擊測試...意圖播報...我認為...

就這樣。 我希望看到“在這里!”。

有什么主意我為什么不呢?

您是否在AndroidManifest.xml中定義了接收器?

另外,當您單擊直接進入撥號程序的內容時,將發送ACTION_CALL_BUTTON。 您確定要這樣做嗎?

我在AndroidManifest.xml中使用了它,並且可以正常工作。 我認為關鍵是意圖過濾器中的意圖NEW_OUTGOING_CALL。

<receiver android:name=".OnMakingCallReceiver" android:exported="true" android:enabled="true">
        <intent-filter android:priority="999">
             <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
             <action android:name="android.intent.action.ACTION_CALL" />  
        </intent-filter>
</receiver>

ACTION_CALL可能會被刪除。 另外,這可能需要添加到清單中:

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

暫無
暫無

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

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