Search code examples
androidskypephone-call

How to open Skype if the device is tablet otherwise make a call action


1)When my app start it shows alert dialog with call button.if it is cell phone than i have to allow user to call.if it is tablet than on click button skype should be open and if skype is not installed open playstore.

public class MainActivity extends Activity {
private Button call_btn, cancle_btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    call_btn = (Button) findViewById(R.id.call_button);
    cancle_btn = (Button) findViewById(R.id.cancel_button);
    call_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:123456789"));
            startActivity(callIntent);

        }
    });

}

}


Solution

  • Give this a try:

    public class MainActivity extends Activity{
    
    public Button call_btn, cancel_btn;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        call_btn = (Button) findViewById(R.id.call_button);
        cancle_btn = (Button) findViewById(R.id.cancel_button);
        call_btn.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                if(deviceIsTablet){
                    if(isAppInstalled("com.skype.android")){
    
                    }else{
    
                    }
                }else{
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:123456789"));
                    startActivity(callIntent);
                }
    
            }
        });
    
    }
    
    // determine whether or no the android device is a tablet or not
    public static boolean deviceIsTablet(Context c) {
        return (c.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }
    
    private boolean isAppInstalled(String app) {
        PackageManager packagemanager = getPackageManager();
        boolean isOnDevice = false;
        try {
           packagemanager.getPackageInfo(app, PackageManager.GET_ACTIVITIES);
           isOnDevice = true;
        } catch (PackageManager.NameNotFoundException exception) {
           isOnDevice = false;
        }
        return isOnDevice;
    }
    

    }