美文网首页Android技术知识Android开发
Android中 Intent 常用跳转第三方软件总结

Android中 Intent 常用跳转第三方软件总结

作者: i小灰 | 来源:发表于2020-12-05 21:32 被阅读0次

Intent 简介:

Intent 是 Android 程序中各组件之间进行交互的一种重要的方式,它不仅可以指明当前组件想要执行的动作,还可以在不同组件间传递数据。Intent 一般可被用于启动活动、启动服务以及发送广播等场景。

Intent 大致分为两种:显式 Intent 和隐式Intent,这里主要介绍显式 Intent 的跳转啊。

   //Activity 常用的页面跳转
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);

拨打电话

一定要加上权限申请,如果 Android 6.0 以及以上,还要动态申请权限

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

  //Activity 拨打电话
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:10086"));
                startActivity(intent);

Intent的 action 是Intent.ACTION_DIAL,这是一个 Android 系统内置动作。然后在 data 部分指定了协议是 tel,号码是 10086。

发送短信

Android 6.0 以及以上除了添加下面这句话,还要进行动态权限的添加,否则报错java.lang.SecurityException: Sending SMS message: uid 10105 does not have android.permission.SEND_SMS。

//权限
<uses-permission android:name="android.permission.SEND_SMS" /> 

第一:调起系统发短信功能
  //Activity 发送短信(一)
                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:10086"));
                intent.putExtra("sms_body", "hello,你好啊");
                startActivity(intent);

第二:调用系统短信接口直接发送短信;
    /**
     * 直接调用短信接口发短信
     *
     * @param phoneNumber
     * @param message
     */
    public void sendSMS(String phoneNumber, String message) {
        // 获取短信管理器
        android.telephony.SmsManager smsManager = android.telephony.SmsManager
                .getDefault();
        // 拆分短信内容(手机短信长度限制)
        List<String> divideContents = smsManager.divideMessage(message);
        for (String text : divideContents) {
            smsManager.sendTextMessage(phoneNumber, null, text, sentPI,
                    deliverPI);
        }

    }

 private PendingIntent deliverPI;
 private PendingIntent sentPI;

 private void init() {
        //处理返回的发送状态
        String SENT_SMS_ACTION = "SENT_SMS_ACTION";
        Intent sentIntent = new Intent(SENT_SMS_ACTION);
        sentPI= PendingIntent.getBroadcast(this, 0, sentIntent,
                0);
        // register the Broadcast Receivers
        this.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context _context, Intent _intent) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(MainActivity.this,
                                "短信发送成功", Toast.LENGTH_SHORT)
                                .show();

                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        break;
                    default:
                }
            }
        }, new IntentFilter(SENT_SMS_ACTION));

        //处理返回的接收状态
        String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
        // create the deilverIntent parameter
        Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
        deliverPI = PendingIntent.getBroadcast(this, 0,
                deliverIntent, 0);
        this.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context _context, Intent _intent) {
                Toast.makeText(MainActivity.this,
                        "收信人已经成功接收", Toast.LENGTH_SHORT)
                        .show();

            }
        }, new IntentFilter(DELIVERED_SMS_ACTION));

    }

打开百度网页

 Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.baidu.com"));
        startActivity(intent);

打开地图

 public void map(){

        String mAddress = "鼓楼大街"  ;
        String mLatitude = "39.948976";
        String mLongitude = "116.393599";
        Uri mapUri = null;
        if (!StringUtil.isEmpty(mAddress)){
            try {
                /**
                 * 有地图软件
                 */
                if (StringUtil.isEmpty(mLatitude) || StringUtil.isEmpty(mLongitude)){
                    mapUri = Uri.parse(StringUtil.getAddressUrl(mAddress));
                }else{
                    mapUri = Uri.parse(StringUtil.getAddressDetailUrl(mLatitude + "",mLongitude+ "",mAddress));
                }
                Intent loction = new Intent(Intent.ACTION_VIEW, mapUri);
                startActivity(loction);
            } catch (ActivityNotFoundException e){
                /**
                 *  没有安装地图软件  会报错  此时打开网页版地图
                 */
                Toast.makeText(this, "ActivityNotFoundException", Toast.LENGTH_SHORT).show();
                if (StringUtil.isEmpty(mLatitude) || StringUtil.isEmpty(mLongitude)){
                    mapUri = Uri.parse(StringUtil.getHtmlAddressUrl(mAddress));
                }else{
                    mapUri = Uri.parse(StringUtil.getHtmlAddressDetailUrl(mLatitude+ "",mLongitude+ "",mAddress));
                }
                Intent loction = new Intent(Intent.ACTION_VIEW, mapUri);
                startActivity(loction);
            } catch (Exception e){
                Toast.makeText(this, "异常", Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(this, "地址为空", Toast.LENGTH_SHORT).show();
        }
    }

常见的Activity Action Intent常量

常量名称 常量值 意义
ACTION_MAIN android.intent.action.MAIN 应用程序入口
ACTION_VIEW android.intent.action.VIEW 显示数据给用户
ACTION_ATTACH_DATA android.intent.action.ATTACH_DATA 指明附加信息给其他地方的一些数据
ACTION_EDIT android.intent.action.EDIT 显示可编辑的数据
ACTION_PICK android.intent.action.PICK 选择数据
ACTION_CHOOSER android.intent.action.CHOOSER 显示一个Activity选择器
ACTION_GET_CONTENT android.intent.action.GET_CONTENT 获得内容
ACTION_DIAL android.intent.action.GET_CONTENT 显示打电话面板
ACITON_CALL android.intent.action.DIAL 直接打电话
ACTION_SEND android.intent.action.SEND 直接发短信
ACTION_SENDTO android.intent.action.SENDTO 选择发短信
ACTION_ANSWER android.intent.action.ANSWER 应答电话
ACTION_INSERT android.intent.action.INSERT 插入数据
ACTION_DELETE android.intent.action.DELETE 删除数据
ACTION_RUN android.intent.action.RUN 运行数据
ACTION_SYNC android.intent.action.SYNC 同步数据
ACTION_PICK_ACTIVITY android.intent.action.PICK_ACTIVITY 选择Activity
ACTION_SEARCH android.intent.action.SEARCH 搜索
ACTION_WEB_SEARCH android.intent.action.WEB_SEARCH Web搜索
ACTION_FACTORY_TEST android.intent.action.FACTORY_TEST 工厂测试入口点

相关文章

网友评论

    本文标题:Android中 Intent 常用跳转第三方软件总结

    本文链接:https://www.haomeiwen.com/subject/mynoiktx.html