我想使用一个按钮拨打电话并使用一个固定号码

I would like to make a call using one button and use one fixed number

我想一键拨打电话。 phone 数字应该在里面。但是我的代码不起作用。谁可以支持?

public class MainActivity 扩展 AppCompatActivity { 私人按钮按钮;

@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>

您的问题是您从未将点击侦听器分配给按钮,因为永远不会调用 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 在调用意图文档中

只需在 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)));
        });

我认为您可以将此字符串添加到清单文件中 <数据android:scheme="电话"/>

// 这个很好用!

((按钮) 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);
    });