home top ad

How to open another app programmatically android

Hello Guys, Today we are going to talk on such a topic, which we do not use anywhere in our app, whether logging in to your app facebook or logging in instagram or whether whatsapp is used, we all use this in our app. Before using it all in our app, we have to check that this app is installed in the user's device or not.
If this app is installed in the user's device, then how will we program the app so that the app is not crashed, we will use if else or try and catch in this article.
An Android app typically has several activities. Each activity displays a user interface that allows the user to perform a specific task (such as view a map or take a photo). To take the user from one activity to another, your app must use an Intent to define your app's "intent" to do something. When you pass an Intent to the system with a method such as startActivity(), the system uses the Intent to identify and start the appropriate app component. Using intents even allows your app to start an activity that is contained in a separate app. You can use this method anywhere.If you want to use this method then follow this code.

This is also useful for you

  • How to Create Image Slider using ViewPager in android studio
  • How to create Swipeable Videos Like TikTok Using ViewPager2 in andriod studio
  • how to get all audio files in android programmatically
  • How to use SearchView in andriod studio
  • How to check internet Connection in androd studio
  • How to Create Bottom Sheet in android studio
  • How to Refresh a list Using RecyclerView in Android Studio
  • How to create Welcome Splash Screen with animation in android studio
  • How to create menu and sub menu in android studio
  • How to create a list item with RecyclerView in Android Studio
  • BottomNavigationView android example
  • How to create Text to Speech app in android studio

  • Method 1: First create new Project in Android Studio
    Start by creating a new Android project in Android studio. Choose "File", "New", select "New Project" and then click "Next". In the New Android Application window, enter your chosen Application, Project, and Package names and then choose "Next" and last finish. After creating the project, you will see two files, first activity_main and second MainActivity.java, then you have to paste the code given below activity_main and MainActivity.java.

    activity_main.xml
     <?xml version="1.0" encoding="utf-8"?>  
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       xmlns:tools="http://schemas.android.com/tools"  
       android:layout_width="match_parent"  
       android:layout_height="match_parent"  
       android:orientation="vertical"  
       tools:context=".MainActivity">  
       <Button  
         android:id="@+id/whatsApp"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_gravity="center_horizontal"  
         android:layout_marginTop="80dp"  
         android:text="open WhatsApp" />  
       <Button  
         android:id="@+id/business_Whats"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_gravity="center_horizontal"  
         android:layout_marginTop="20dp"  
         android:text="open WA Business" />  
       <Button  
         android:id="@+id/facebook"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_gravity="center_horizontal"  
         android:layout_marginTop="20dp"  
         android:text="open facebook" />  
       <Button  
         android:id="@+id/instagram"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_gravity="center_horizontal"  
         android:layout_marginTop="20dp"  
         android:text="open instagram" />  
     </LinearLayout>  
    
    Here you are seeing four buttons on which we will use OnClickListener so that we will be able to open different aap. Such as whatsapp, facebook, instagram and whatsapp business.
    After creating activity_main, now in MainActivity.java we will use OnClickListener on these buttons so that we will be able to open a different app.

    MainActivity.java
     import androidx.appcompat.app.AppCompatActivity;  
     import android.content.ActivityNotFoundException;  
     import android.content.Intent;  
     import android.content.pm.PackageManager;  
     import android.os.Bundle;  
     import android.view.View;  
     import android.widget.Button;  
     import android.widget.Toast;  
     public class MainActivity extends AppCompatActivity {  
       private Button wa,ba,fa,in;  
       @Override  
       protected void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.activity_main);  
         wa=findViewById(R.id.whatsApp);  
         ba=findViewById(R.id.business_Whats);  
         fa=findViewById(R.id.facebook);  
         in=findViewById(R.id.instagram);  
         wa.setOnClickListener(new View.OnClickListener() {  
           @Override  
           public void onClick(View v) {  
             String whatsAppPackageName="com.whatsapp";  
             if (checkInstall(whatsAppPackageName)){  
               openApp(whatsAppPackageName);  
             }  
             else {  
               Toast.makeText(MainActivity.this, "WhatsApp Not Install", Toast.LENGTH_SHORT).show();  
             }  
           }  
         });  
         ba.setOnClickListener(new View.OnClickListener() {  
           @Override  
           public void onClick(View v) {  
             String whatsAppPackageName="com.whatsapp.w4b";  
             if (checkInstall(whatsAppPackageName)){  
               openApp(whatsAppPackageName);  
             }  
             else  
             {  
               Toast.makeText(MainActivity.this, "WhatsApp Business Not Install", Toast.LENGTH_SHORT).show();  
             }  
           }  
         });  
         fa.setOnClickListener(new View.OnClickListener() {  
           @Override  
           public void onClick(View v) {  
             String fa="com.facebook.katana";  
             if (checkInstall(fa)){  
               openApp(fa);  
             }  
             else  
             {  
               Toast.makeText(MainActivity.this, "Facebook Not Install", Toast.LENGTH_SHORT).show();  
             }  
           }  
         });  
         in.setOnClickListener(new View.OnClickListener() {  
           @Override  
           public void onClick(View v) {  
             String insta="com.instagram.android";  
                 if (checkInstall(insta))  
                 {  
                   openApp(insta);  
                 }  
                 else {  
                   Toast.makeText(MainActivity.this, "Instagram not Not Install", Toast.LENGTH_SHORT).show();  
                 }  
           }  
         });  
     }  
       private boolean checkInstall(String whatsAppPackageName) {  
           PackageManager pm = getPackageManager();  
           boolean app_installed = false;  
           try {  
             pm.getPackageInfo(whatsAppPackageName, PackageManager.GET_ACTIVITIES);  
             app_installed = true;  
           } catch (PackageManager.NameNotFoundException e) {  
             app_installed = false;  
           }  
           return app_installed;  
         }  
       private void openApp(String whatsAppPackageName) {  
         Intent intent = getPackageManager().getLaunchIntentForPackage(whatsAppPackageName);  
         startActivity(intent);  
       }  
     }  
    
     <?xml version="1.0" encoding="utf-8"?>  
     <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
       package="com.example.sharelistproject"> 
     <queries>
            <package android:name="com.whatsapp" />
            <package android:name="com.whatsapp.w4b" />
           <package android:name="com.instagram.android" />
           <package android:name="com.facebook.katana" />
        </queries>
        <queries>
            <intent>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="https" />
            </intent>
            <intent>
                <action android:name="android.support.customtabs.action.CustomTabsService" />
            </intent>
        </queries> 
       <application  
         android:allowBackup="true"  
         android:icon="@mipmap/ic_launcher"  
         android:label="@string/app_name"  
         android:roundIcon="@mipmap/ic_launcher_round"  
         android:supportsRtl="true"  
         android:theme="@style/Theme.ShareListProject">  
         <activity android:name=".MainActivity">  
           <intent-filter>  
             <action android:name="android.intent.action.MAIN" />  
             <category android:name="android.intent.category.LAUNCHER" />  
           </intent-filter>  
         </activity> 
       </application>  
     </manifest>
    if app not open then use package name in manifest inside queries.
    Here we are using four buttons and calling the method to open the app using OnClickListener on all the buttons.
    In every OnclickListener, first of all we will assign the package name of the app to the string we want to open in that variable.
    After that in the if condition we will call the checkInstall method which will take a string type parameter (package name of app) which will check every time that the app user's device is installed or not if the app is installed inside the body of the if condition If the app is not installed, then another method will run, in which a toast will be printed that the app is not found or that you want to print.
    Similarly, by assigning the package name of all the app to a string variable, checkinstall in if else and call the openApp method and give it app package name.If you do not want to assign the app package name to a string, then instead of the app string, write the package name directly under "write package name".
    Below is a screenshot video of it, so that you will know how the app will work.
    I hope this article will be very useful for you.
    Thanks You

Post a Comment

0 Comments