Created
September 19, 2020 12:07
-
-
Save AhmedMaad/6f6b48c4d3f6ba9d3edc1da2f402aa51 to your computer and use it in GitHub Desktop.
Android Java code for calling a number without the need to open the dialer before calling to make a faster process in emergency apps.
This file contains hidden or 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
//TODO: Add your package name here (e.g. com.maad.candroid) | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.core.app.ActivityCompat; | |
import android.Manifest; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.view.View; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
public void callButton(View view) { | |
/*Starting from Android Marshmallow 6.0, we have to request every dangerous permission via code | |
Before 6.0: https://developer.android.com/guide/topics/permissions/overview#install-time_requests_android_511_and_below | |
6.0 and higher: https://developer.android.com/guide/topics/permissions/overview#runtime_requests_android_60_and_higher | |
so, in this if statement condition we are checking if the request has been granted, if the condition is "true" which | |
means that the permission is granted, so we will make our intent, and if the condition is "false" then we will show | |
a request permission dialog via the requestPermissions method. | |
*/ | |
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { | |
//You can use: | |
Intent phoneIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" /*+ NUMBER_TO_CALL*/)); | |
startActivity(phoneIntent); | |
//or you can use this alternative: | |
//Intent phoneIntent = new Intent(Intent.ACTION_CALL); | |
//phoneIntent.setData(Uri.parse("tel:" /*+ NUMBER_TO_CALL*/)); | |
//startActivity(phoneIntent); | |
} else { | |
requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 101); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment