Question

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);

        }
    });

}

}

Was it helpful?

Solution 4

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;
}

}

OTHER TIPS

Using this method, you can actually find out if it's a tablet or Phone:

public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

So code that you already wrote should work fine for phones to make calls using phone dialer. For tablets, you can use this method to make skype call:

public static void skype(String number, Context ctx) {
        try {
           Intent sky = new Intent("android.intent.action.VIEW");
            sky.setData(Uri.parse("skype:" + number));
            ctx.startActivity(sky);
        } catch (ActivityNotFoundException e) {
           //Skype not installed
        }

    }

ActivityNotFoundException can be caught which means Skype isn't installed.

So you can use this code to take the user to Playstore:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=com.skype.raider" )));

P.S: If your only requirement is to find if it's a tablet or phone, the above method works. If you want to find if Sim is present or any such stuff, you can use code from other answers.

Below is a quick example to check if the device is a phone or a tablet

TelephonyManager manager = 
                  (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

if (manager.PhoneType == PhoneType.None) {
            // Not a phone, open Skype
}
else {
           // It is a phone, show dial
}

You can take it bit further by making sure that a SIM card is available

if (manager.getSimState() != TelephonyManager.SIM_STATE_ABSENT){
    // Open skype
} else {
   // Can dial
}

Combine this answer with other answers that show you how to start Skype and you should have a working solution!

You can follow the step to achieve the requirements.

Step1: You can keep this code under your button click or in onCreate.

    if(isTelePhonyAvailable()){
        //Do Telephony calling stuff 
    } else if(isSkypeInstalled()){
        //Do Skype calling stuff
    } else {
        //Open google play to download the skype application
        openGooglePlayToDownloadSkype();
    }

Step2: Just copy paste these methods in your activity file. Method name will give clear message what they are doing.

private boolean isTelePhonyAvailable(){
    TelephonyManager tm= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    if(tm.getPhoneType()==TelephonyManager.PHONE_TYPE_NONE){
        return false;
    }
    return true;
}

private boolean isSkypeInstalled(){
    boolean isInstalled = false;
    String appPackageName = "com.skype.raider";

    PackageManager pm = getPackageManager();
    try {
        pm.getPackageInfo(appPackageName, PackageManager.GET_ACTIVITIES);
        isInstalled = true;
    } catch (PackageManager.NameNotFoundException e) {
        isInstalled = false;
    }
    return isInstalled;
}

private void openGooglePlayToDownloadSkype(){
    String appPackageName = "com.skype.raider";
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
}

Step3: Finally you require some permissions.

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Hope it will help you..:)

Try this way

Intent intent = new Intent(Intent.ACTION_CALL);
PackageManager manager = mContext.getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(intent, 0);

if (list != null && list.size() > 0) {
        //At least one activity available to make calls
} else {
        //No activity to handle calls.//start skype call
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top