2

当用户使用标准 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>

我单击测试按钮,我看到了这个输出。

点击测试...意图广播...我想...

就这样。我期待看到“到了这里!”。

任何想法为什么我不?

4

2 回答 2

0

Did you define the receiver in your AndroidManifest.xml?

Also, ACTION_CALL_BUTTON is sent when you click on something that goes directly to the dialer. Are you sure you're doing that.

于 2012-11-21T22:13:17.560 回答
0

我在 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" /> 
于 2012-12-23T20:21:26.717 回答