【问题标题】:Android Call Application "how to"Android 呼叫应用程序“如何”
【发布时间】:2014-03-25 11:28:22
【问题描述】:

我有这个非常基本的代码,应该可以用作呼叫应用程序。 一切都是硬编码的。

根据我需要/想要创建的这个应用程序,我没有找到有用的教程。

我的问题很大!谁能帮我创建一个 CallApplication

要求我觉得很简单

需要能够拨打号码

这是我目前拥有的代码,据说它非常基本,但我被卡住了^^

非常感谢任何帮助!

import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.telephony.PhoneStateListener;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class MainActivity extends Activity {

        final Context context = this;
        private Button btn;

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            btn = (Button) findViewById(R.id.button);

            PhoneCallListener phoneCallListener = new PhoneCallListener();
            TelephonyManager telManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
            telManager.listen(phoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);

            // add button listener
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {

                    Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
                    phoneCallIntent.setData(Uri.parse("tel:123456"));
                    startActivity(phoneCallIntent);

                }

            });

        }

        // monitor phone call states
        private class PhoneCallListener extends PhoneStateListener {

            String TAG = "LOGGING PHONE CALL";

            private boolean phoneCalling = false;

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {

                if (TelephonyManager.CALL_STATE_RINGING == state) {
                    // phone ringing
                    Log.i(TAG, "RINGING, number: " + incomingNumber);
                }

                if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                    // active
                    Log.i(TAG, "OFFHOOK");

                    phoneCalling = true;
                }

                // When the call ends launch the main activity again
                if (TelephonyManager.CALL_STATE_IDLE == state) {

                    Log.i(TAG, "IDLE");

                    if (phoneCalling) {

                        Log.i(TAG, "restart app");

                        // restart app
                        Intent i = getBaseContext().getPackageManager()
                                .getLaunchIntentForPackage(
                                        getBaseContext().getPackageName());

                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i);

                        phoneCalling = false;
                    }

                }
            }
        }

    }

【问题讨论】:

  • 就像你在代码中的某个点...我得到了一个硬编码的联系人。 ---phoneCallIntent.setData(Uri.parse("tel:123456"));--- 这必须是动态的......我的意思是......而不是硬编码......我需要能够拨号一个号码,然后拨打我已拨打的号码……你看到了吗? xD ...

标签: android call


【解决方案1】:

根据@SeeSharp 评论。

我猜你的要求是删除硬编码的联系人。

方式一:

将联系人作为用户输入(EditText 中的示例,设计 UI)。

方式二:

使用Getting a Result from an Activity 从通讯录应用中获取联系人。

startActivityForResult() 联系人应用程序从android.provider.ContactsContract 中选择联系人。更多信息:thisthis

阅读ContactsContract


注意:请使您的问题/所讨论的问题足够解释清楚。

希望这会有所帮助!!

【讨论】:

  • 嗯,不是真的,也许有人想拿着我的 ahdn 给我一个完整的解释...--也许是他自己的代码或编程方式?--已经快一周了,我仍然还没想通。谁有一个应用程序(自定义)的示例,它允许我拨打一个号码并在现实生活中拨打该号码?请..非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多