Last active
February 26, 2024 07:41
-
-
Save sunmeat/bf69ca2d8e2a41f31fd554b4a8447936 to your computer and use it in GitHub Desktop.
action call example android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AndroidManifest.xml: | |
... | |
<uses-feature | |
android:name="android.hardware.telephony" | |
android:required="false" /> | |
<uses-permission android:name="android.permission.CALL_PHONE" /> | |
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> | |
... | |
========================================================================================== | |
MainActivity.java: | |
package com.sunmeat.call; | |
import android.Manifest; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.net.Uri; | |
import android.os.Bundle; | |
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_PHONE_PERMISSION = 1; | |
private String phoneNumber = "tel:" + "+380630300123"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri.parse(phoneNumber)); | |
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { | |
startActivity(dialIntent); | |
} else { | |
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL_PHONE_PERMISSION); | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
if (requestCode == REQUEST_CALL_PHONE_PERMISSION) { | |
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri.parse(phoneNumber)); | |
startActivity(dialIntent); | |
} else { | |
Toast.makeText(this, "разрешение не получено, очень жаль", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment