【问题标题】:I would like to make a call using one button and use one fixed number我想一键拨打电话,使用一个固定号码
【发布时间】:2021-07-15 23:52:19
【问题描述】:

我想使用一键拨打电话。电话号码应该在里面。但是我的代码不起作用。谁能支持?

公共类 MainActivity 扩展 AppCompatActivity { 私有Button按钮;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button) findViewById(R.id.button);
}

public void onClickStart(View view) {
    button = findViewById(R.id.button);
   
    ((Button)findViewById(R.id.button)).setOnClickListener((View.OnClickListener) v -> {

        Intent callIntent = new Intent(Intent.ACTION_CALL);
        String phNum = "tel:" + "89261234567";
        callIntent.setData(Uri.parse(phNum));
        startActivity( callIntent) ;
    });

}

}

清单文件

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.GateApp">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

【问题讨论】:

    标签: java android phone-call


    【解决方案1】:

    您的问题是您从未将单击侦听器分配给按钮,因为从未调用过 onClickStart。你应该这样做

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener((View.OnClickListener) v -> {
    
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            String phNum = "tel:" + "89261234567";
            callIntent.setData(Uri.parse(phNum));
            startActivity( callIntent) ;
        });
    }
    

    这样,您将点击侦听器分配给按钮。在documentation你可以找到它的使用方法。

    Here it is the link 在通话意图文档中

    【讨论】:

      【解决方案2】:

      只需在 onClickListener 中使用这两行代码:

       ((Button)findViewById(R.id.button)).setOnClickListener((View.OnClickListener) v -> {
                  String phNum = "tel:" + "89261234567";
                  startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phNum, null)));
              });
      

      【讨论】:

        【解决方案3】:

        我认为您可以将此字符串添加到清单文件中

        【讨论】:

          【解决方案4】:

          // 这很好用!

          ((Button) findViewById(R.id.button1)).setOnClickListener((View.OnClickListener) v -> {

                  Intent callIntent = new Intent(Intent.ACTION_CALL);
                  String phNum = "tel:" + "87777777777";
                  callIntent.setData(Uri.parse(phNum));
                  startActivity(callIntent);
              });
          

          【讨论】:

            猜你喜欢
            • 2022-10-21
            • 1970-01-01
            • 1970-01-01
            • 2018-03-09
            • 1970-01-01
            • 2020-10-31
            • 1970-01-01
            • 1970-01-01
            • 2016-08-11
            相关资源
            最近更新 更多