首页 > 解决方案 > 我想一键拨打电话,使用一个固定号码

问题描述

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

公共类 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>

标签: javaandroidphone-call

解决方案


// 这很好用!

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

推荐阅读