Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.phonecall;
- import android.Manifest;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.Toast;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.ActivityCompat;
- import androidx.core.content.ContextCompat;
- public class MainActivity extends AppCompatActivity {
- private static final int REQUEST_CALL = 1;
- private EditText mEditTextNumber;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //mEditTextNumber = findViewById(R.id.edit_text_number);
- ImageView imageCall = findViewById(R.id.image_call);
- Button btn = findViewById(R.id.continuous);
- imageCall.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- makePhoneCall();
- }
- });
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- for (int i = 0; i < 10; i++) makePhoneCall();
- }
- });
- }
- private void makePhoneCall() {
- //String number = mEditTextNumber.getText().toString();
- String number = "01893713798";
- if (number.trim().length() > 0) {
- if (ContextCompat.checkSelfPermission(MainActivity.this,
- Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(MainActivity.this,
- new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL);
- } else {
- String dial = "tel:" + number;
- startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));
- }
- } else {
- Toast.makeText(MainActivity.this, "Enter Phone Number", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if (requestCode == REQUEST_CALL) {
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- makePhoneCall();
- } else {
- Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
- }
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:tools="http://schemas.android.com/tools"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity">
- <!-- <EditText-->
- <!-- android:id="@+id/edit_text_number"-->
- <!-- android:layout_width="match_parent"-->
- <!-- android:layout_height="wrap_content"-->
- <!-- android:layout_alignParentStart="true"-->
- <!-- android:layout_alignParentTop="true"-->
- <!-- android:layout_marginTop="147dp"-->
- <!-- android:inputType="phone" />-->
- <ImageView
- android:id="@+id/nothing"
- android:layout_width="match_parent"
- android:layout_height="277dp"
- android:layout_marginTop="25dp"
- android:src="@drawable/abc" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Call Anonna"
- android:textSize="40dp"
- android:layout_gravity="center"
- android:layout_marginTop="10dp"
- android:textColor="#ff0000"/>
- <ImageView
- android:id="@+id/image_call"
- android:layout_width="80dp"
- android:layout_height="80dp"
- android:layout_marginTop="20dp"
- android:layout_gravity="center"
- android:src="@drawable/ic_phone" />
- <Button
- android:id="@+id/continuous"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="5dp"
- android:text="Continuous Call"
- android:layout_marginTop="20dp"
- android:layout_marginLeft="30dp"
- />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment