Android Intent.ACTION_CALL, Uri

前端 未结 9 730
南笙
南笙 2020-12-05 16:12

I am trying to use the Intent.Action class. I know how to use the ACTION_VIEW to display a URL but I wanted to use the Intent.ACTION_DIAL to call number when th

9条回答
  •  感情败类
    2020-12-05 16:40

    For ACTION_DIAL you just need to create Intent object with that action as a first argument and Uri object as a second argument built from phone number written as a string. After that just call startActivity() method and pass previously created intent object as an argument. For example:

    private String phoneNumber = "123456";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Button dial_number = findViewById(R.id.button);
        dial_number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
                startActivity(intent);
            }
        });
    }
    

提交回复
热议问题