Posts

Showing posts with the label androidstudio

[Android Studio] Call and Hang Up on Android Programmatically

Steps to make a phone call and hang up on android the phone programmatically. 1. Add permission to make a phone call. < uses-permission android :name ="android.permission.CALL_PHONE" /> 2. A function to make a call to the phone_no . void PhoneCall(String phone_no){ Intent intent = new Intent(Intent. ACTION_CALL ); LogWrapper. d ( "phonecall" ,phone_no); intent.setData(Uri. parse ( "tel:" +phone_no)); startActivity(intent); } 3. In order to hang up the call programmatically, the device has to be rooted. Such action of modifying phone state is a system permission, which is not available to 3rd party applications. Use SuperSu to execute the command to hang up as a root. void EndCall(){ try { Process root = Runtime. getRuntime ().exec( "su && input keyevent 6" ); } catch (IOException e) { e.printStackTrace(); Toast. makeText ( this , "failed to end the call" , Toast. LENG...

[Android Studio] How to Call REST API with Retrofit

Retrofit is a REST Client library (Helper Library) used in Android and Java to create an HTTP request and also to process the HTTP response from a REST API. We can also use retrofit to receive data structures other than JSON, for example SimpleXML and Jackson. Advantages of using Retrofit - It manages network communication systematically and efficiently. - You can request data in certain format, and parse the response data. - It is relatively easier to handle errors. Here are the steps to call REST API using Retrofit in Android. 1. Add INTERNET permission in AndroidManifest.xml <uses-permission android:name= "android.permission.INTERNET" /> 2. Open Gbuild.gradle (:app) and add the libraries inside the dependencies. implementation 'com.squareup.retrofit2:retrofit:2.1.0' implementation 'com.squareup.retrofit2:converter-gson:2.0.0' 3. Create an APIResponse class to parse the result data later.     In my case, the respon...

[AndroidStudio] How to Root Android Studio Emulator (AVD)

Image
* Change  red-colored names . 1. Create a new AVD. (named Nexus6 here) 2. Check the directory of Android SDK ($SDK_PATH) and start emulator with args as following. $ SDK_PATH/emulator/emulator -avd Nexus6 -writable-system -selinux permissive -qemu -enable-kvm    In my case, "-qemu -enable-kvm" didn't work on MacOS, so I skipped those args. 3. If the device is booted, restart adbd as root and remount system. $ adb root $ adb remount      If adb command doesn't exist, make sure to add PATH.    Mine was located in ~/Library/Android/sdk/platform-tools. 4. Download and install Superuser.apk $ git clone https://github.com/0xFireball/root_avd.git $ cd root_avd $ adb install SuperSU/common/Superuser.apk 5. Pick the corresponding architecture and save it as variable $ARCH.     Then push su, update permissions, and set SELinux peprmissive. $ ARCH= x86 $ adb push SuperSU/$ARCH/su/...

[AndroidStudio] AVD - adbd cannot run as root in production builds

Image
I had a trouble getting a root access to Android Studio Emulator (AVD), because I couldn't run  adb root  as following. $ adb root adbd cannot run as root in production builds My device was Pixel 2 with the system image of Android 9.0 (Google Play). However, ADB root is not working on emulator with Google Play image. So I created a new device, and for the system image, I selected one with GoogleAPIs , not Google Play. When I tried again with the new device, I was able to successfully run  adb root . $ adb root restarting adbd as root

[AndroidStudio] Calling Button / Permission Check & Request

1. Add user permission in manifest file [AndroidManifest.xml] < uses-permission  android :name ="android.permission.CALL_PHONE" ></ uses-permission > 2. Check if the app has permission to call. If not, request permission. 3. Set a listener on the button. 4. Create a onClick function to trigger the call. 5. Create new Intent and and start activity. public class MainActivity extends AppCompatActivity { private final int MY_PERMISSIONS_CALL_PHONE = 1001 ; @Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . layout . activity_main ); try { if ( ActivityCompat . checkSelfPermission ( this , Manifest . permission . CALL_PHONE ) != PackageManager . PERMISSION_GRANTED ) { Toast . makeText ( this , "No permission to call!" , Toast . LENGTH_SHORT ). show (); ActivityCo...