如果设备是平板电脑,如何打开Skype,否则进行呼叫操作

时间:2014-03-18 07:16:59

标签: android skype phone-call

1)当我的应用程序启动时,它显示带有呼叫按钮的警报对话框。如果它是手机而不是我必须允许用户呼叫。如果它是平板电脑而不是点击按钮skype应该打开并且如果没有安装skype Play商店中。

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

        }
    });

}

}

5 个答案:

答案 0 :(得分:2)

使用此方法,您实际上可以确定它是平板电脑还是手机:

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

因此,您已经编写的代码应该可以正常使用手机来拨打电话。对于平板电脑,您可以使用此方法进行Skype调用:

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可以被捕获,这意味着Skype尚未安装。

因此,您可以使用此代码将用户带到Playstore:

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

P.S :如果您的唯一要求是查找是平板电脑还是手机,则上述方法有效。如果您想查找Sim是否存在或任何此类内容,您可以使用其他答案中的代码。

答案 1 :(得分:1)

以下是检查设备是手机还是平板电脑的快速示例

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

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

您可以通过确保 SIM 卡可用来进一步了解

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

将此答案与显示如何启动Skype的其他答案相结合,您应该有一个可行的解决方案!

答案 2 :(得分:1)

您可以按照步骤来达到要求。

第1步:您可以将此代码保留在按钮单击或onCreate下。

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

第2步:只需将这些方法复制粘贴到您的活动文件中即可。方法名称将清楚地表明他们正在做什么。

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

第3步:最后您需要一些权限。

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

希望它会对你有所帮助.. :)

答案 3 :(得分:0)

试试这种方式

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
}

答案 4 :(得分:0)

尝试一下:

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

}