如何检测我的应用程序是否启动了Intent

时间:2014-01-08 09:01:44

标签: android android-intent broadcastreceiver android-alertdialog

我有一个基本上监听所有传出呼叫意图的类,并向用户显示一个对话框。 如果用户点击“是”,那么我让他通过电话网络完成呼叫,发出CALL意图并解雇它。

现在,每当触发此自定义调用意图时,我的侦听器也会再次被触发,并显示对话框。我想知道,在我的广播接收器监听器中,如果通过我的应用程序或其他应用程序拨打电话,我如何识别。

我尝试在自定义调用intent中传递一个额外的键值参数,但在listner中,缺少该键值条目。

BroadcastReceiver Listner

    public class CallListener extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            String phoneNumber = intent.getExtras().getString(Intent.EXTRA_PHONE_NUMBER);
            if(intent.getExtras().containsKey("myapp"))
                Log.e("randomstring"," key present " +phoneNumber);
            else
                Log.e("randomstring","key absent "+phoneNumber); // always absent even if I add if before starting call intent.
            if (phoneNumber != null) {
                setResultData(null);
                setResultCode(0);
                Intent intentvar = new Intent(context, OutgoingCall.class);
                intentvar.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intentvar);
            }
        }
    }
}

要显示对话框的活动类

    public class OutgoingCall extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        displayAlert();
    }

    private void displayAlert() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_Holo_Light_DarkActionBar);
        builder.setMessage("Do you really want to call him/her?").setCancelable(
                false).setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                        String url = "tel:00000000"; //need to pass the number via intent extras.
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
                        intent.putExtra("myapp", true);
                        startActivity(intent);
                        finish();
                    }
                }).setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                        finish();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

0 个答案:

没有答案