呼出不启动呼出

2023-09-07 15:04:51 作者:在思念里沉沦

我正在写一个应用程序,:

I'm writing an application that:

拦截拨出电话

Intercepts an outgoing call

显示一个对话框,询问该呼叫是否是个人或企业(Aziendale在意大利语)

Shows a dialog asking whether the call is "personal" or "business" ("Aziendale" in italian)

如果个人,使给定数量的通话 如果生意,prepends后缀的数字(例如4888 - 只是暂时在我的code) If "personal", makes the call with the given number If "business", prepends a suffix to the number (for example 4888 - just temporarily in my code)

的一点是:我不知道如何拨打电话等待用户的选择,我:

The point is: as I don't know how to make the call wait for the user's choice, I:

关闭来电与 setResultData(空)

显示一个警告对话框

在用户preSS一键拨打电话

After user press one button make the call

但是,当它的时间来拨打电话,什么都不会发生。

But, when it's time to make the call, nothing happens.

让我描述我的应用程序的类别:

Let me describe the classes of my application:

MainActivity (标准自创建活动目前没有任何功能 - 将提高通话时工作)

MainActivity (standard self-created activity for now without any function - will improve when calls work)

OutgoingCallReceiver (即扩展的类的BroadcastReceiver - 拦截拨出电话)

OutgoingCallReceiver (the class that extends BroadcastReceiver - intercept the outgoing calls)

AlertActivity (主题作为一个警告对话框,显示警报)

AlertActivity (themed as an alert dialog, show the alert)

CallActivity (称为 AlertActivity - 要拨打电话)

CallActivity (called by AlertActivity - should make the call)

清单文件是:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.simplecall"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="OutgoingCallReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <activity
            android:name="com.example.simplecall.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name="com.example.simplecall.AlertActivity"
            android:theme="@android:style/Theme.Dialog" >
        </activity>
        <activity 
            android:name="com.example.simplecall.CallActivity" >
        </activity>
    </application>

</manifest>

下面是我的codeD我的课:

Here is how I coded my classes:

MainActivity

MainActivity:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    String numero;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

OutgoingCallReceiver

OutgoingCallReceiver:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class OutgoingCallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub


        // il numero che si stava per chiamare
        final String numero = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);


        Intent in = new Intent(context,AlertActivity.class);
        in.putExtra("com.example.simplecall.numero", numero);
        in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        setResultData(null);
        context.startActivity(in);

        //Toast toast = Toast.makeText(context, "Chiamata verso: " + numero, 1500);
        //toast.show();
    }

}

AlertActivity

AlertActivity:

import android.app.Activity; 
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

public class AlertActivity extends Activity {
    String numero;
    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        numero = getIntent().getStringExtra("com.example.simplecall.numero");
        Toast toast = Toast.makeText(this, "numero : " + numero, 5000);
        toast.show();
        showSettingsAlert();


    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

     /**
     * Mostra una finestra di dialogo
     * Cliccando su Impostazioni si accede al menù di configurazione 
     * */
    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        // Titolo della finestra
        alertDialog.setTitle("Tipo di chiamata");

        // Mostra l'avvertimento
        alertDialog.setMessage("Che tipo di chiamata effettuare?");

        // Cliccando su Impostazioni ...
        alertDialog.setPositiveButton("Aziendale", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                numero = "tel:4888"+numero;
                /*
                 Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse(numero));
                //dialog.dismiss();
                startActivity(callIntent);
               */
                Intent in = new Intent(mContext,CallActivity.class);
                in.putExtra("com.example.simplecall.numero", numero);
                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(in);
                dialog.dismiss();
                finish();
            }
        });

        // Cliccando su Personale
        alertDialog.setNegativeButton("Personale", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                numero = "tel:" + numero;
                /*
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse(numero));
                //dialog.dismiss();

                startActivity(callIntent);
                */
                Intent in = new Intent(mContext,CallActivity.class);
                in.putExtra("com.example.simplecall.numero", numero);
                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(in);
                dialog.dismiss();
                finish();
            }
        });

        // Mostra la finestra di alert
        alertDialog.show();
    }

    private void makeCall(String number, DialogInterface dial) {
        dial.dismiss();
        try {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:"+number));
            startActivity(callIntent);
            finish();
        } catch (ActivityNotFoundException activityException) {
             activityException.printStackTrace();
        }
    }

}

CallActivity

CallActivity:

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class CallActivity extends Activity
{
String numero; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    numero = getIntent().getStringExtra("com.example.simplecall.numero");
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse(numero));
    //finish();
    startActivity(callIntent);

}

}

但问题是,当我实例 CallActivity 什么也没有发生......任何提示?

But the problem is that when I instantiate CallActivity nothing happens ... any hints?

我创建了一个新的应用程序,只是如果我做所有正确的方式,在清单文件相同的权限测试,但它有一个标准的自我创建的活动,其中i执行

I created a new "application", just for testing if i'm doing all in the correct way, same permissions in the manifest file, but it have just a standard self created activity where i perform

Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:xxx")); // xxx is a real number in the code
    finish();
    startActivity(callIntent);

和一切工作正常,不明白的地方我失败了。

and all works fine, don't understand where i fail.

我也注意到,如果因任何原因推两个按钮中的一个之后,应用程序崩溃(例如,如果我拨打电话的onDestroy()不调用super.ondestroy()),呼叫将被正确执行

I also noticed that if for any reason the application crash after pushing one of the two buttons(for example, if i make the call onDestroy() without calling the super.ondestroy()) , the call is correctly performed

推荐答案

一旦你看着办吧,问题其实很简单.. 你有一个广播接收器拨出电话,这拦截他们,并显示您的对话框。 在选择呼叫是私人或企业,你把电话与修改后的号码......猜猜谁截取的电话吗?您的广播接收机,和因此该循环的推移。 芹苴prevent这个无限循环,我所知道的唯一的办法,就是将修改后的通话,并在通话结束后重新启用之前禁用广播接收器。

The problem is actually quite simple once you figure it out.. You have a broadcast receiver for outgoing calls, which intercepts them and shows your dialog. After you choose if the call is private or business, you place the call with the modified number...and guess who intercepts the call? Your broadcast receiver, and so the loop goes. Tho prevent this infinite loop, the only way I know of, is to disable the broadcast receiver before placing the modified call, and enabling it again after the call ends.

private void makeCall1(String number)  {
    PackageManager pm = mContext.getPackageManager();
    ComponentName componentName = new ComponentName(mContext, OutgoingCallReceiver.class);
    pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number));
    startActivity(callIntent);
    // Now wait for the call to end somehow and afterwards ->
    // pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}

我希望我的帮助!

I hope I was of help!