简体   繁体   English

广播接收器未收到意图

[英]Intent not received by Broadcast Receiver

I'm trying to show a message when a user places a call using the standard android dialer. 我正在尝试在用户使用标准android拨号器拨打电话时显示一条消息。

I have the following code in my Activity 我的活动中包含以下代码

 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... ");
        }
     });
     }

Then in the BroadcastReceiver (well a class that derives from it): 然后在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!");       
    }
}

And then in the AndroidManifest.xml 然后在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>

I click the test button and I see this output. 我单击测试按钮,然后看到此输出。

Clicked on test... intent broadcasted... I think... 点击测试...意图播报...我认为...

And thats all. 就这样。 I expected to see "got here!". 我希望看到“在这里!”。

Any ideas why I don't? 有什么主意我为什么不呢?

Did you define the receiver in your AndroidManifest.xml? 您是否在AndroidManifest.xml中定义了接收器?

Also, ACTION_CALL_BUTTON is sent when you click on something that goes directly to the dialer. 另外,当您单击直接进入拨号程序的内容时,将发送ACTION_CALL_BUTTON。 Are you sure you're doing that. 您确定要这样做吗?

I used this in AndroidManifest.xml and it works. 我在AndroidManifest.xml中使用了它,并且可以正常工作。 I think the key thing is the intent NEW_OUTGOING_CALL in the intent-filter. 我认为关键是意图过滤器中的意图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>

Probably ACTION_CALL could be removed. ACTION_CALL可能会被删除。 Also this may need to be added to the manifest: 另外,这可能需要添加到清单中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM