用过的Intent的用法

本文详细介绍了如何使用 Android 的 Intent 来实现多种功能,包括打电话、发短信、发邮件、启动相机、安装 APK 文件等。此外还提供了启动 QQ、打开地图应用、启动录音机等具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址 http://blog.youkuaiyun.com/qq_25806863/article/details/70145785

突然看到之前的一篇笔记,就补充一下发上来。Intent是什么就先不说了。列表可以看一下目录。

所有用法均在小米max手机上亲测,android6.0。有些权限在6.0上要动态获取。

以后如果用到新功能了再补充,没用过的先不写了。

打电话

使用ACTION_CALL需要android.permission.CALL_PHONE权限

    Intent intent=new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    //intent.setAction(Intent.ACTION_CALL);
    //intent.setAction(Intent.ACTION_DIAL);
    intent.setData(Uri.parse("tel:12212212212"));
    startActivity(intent);

发短信

  • 1
    Intent intent= new Intent();
    intent.setData(Uri.parse("smsto:10086"));
    intent.setAction(Intent.ACTION_SENDTO);
    intent.putExtra("sms_body", "填信息内容");
    startActivity(intent);
  • 2
Intent intent= new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setType("vnd.android-dir/mms-sms");
                intent.putExtra("sms_body", "填信息内容");
                startActivity(intent);

发邮件

  • 弹出选择器
Intent intent=new Intent();
                intent.setAction(Intent.ACTION_SEND);
                //收件人
                String[] tos={"wangyisll@163.com"};
                //抄送人
                String[] ccs={"1212121@qq.com"};
                intent.putExtra(Intent.EXTRA_EMAIL, tos);
                intent.putExtra(Intent.EXTRA_CC, ccs);
                intent.putExtra(Intent.EXTRA_TEXT, "邮件内容 2333");
                intent.putExtra(Intent.EXTRA_SUBJECT, "邮件主题");
                intent.setType("message/rfc822");
                startActivity(Intent.createChooser(intent, "选择客户端发送"));
  • 直接启动邮件客户端
Intent intent=new Intent();
                intent.setData(Uri.parse("mailto:"));
                intent.setAction(Intent.ACTION_SENDTO);
                //收件人
                String[] tos={"wangyisll@163.com"};
                //抄送人
                String[] ccs={"1212121@qq.com"};
                intent.putExtra(Intent.EXTRA_EMAIL, tos);
                intent.putExtra(Intent.EXTRA_CC, ccs);
                intent.putExtra(Intent.EXTRA_TEXT, "邮件内容 2333");
                intent.putExtra(Intent.EXTRA_SUBJECT, "邮件主题");
                //intent.setType("message/rfc822");
                startActivity(intent);

启动相机

  • 单纯启动相机
Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivity(intent);
  • 以录像模式启动相机
Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
startActivity(intent);
  • 获取拍照返回的缩略图 需要android.permission.CAMERA权限
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,0);

然后在

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bitmap bitmap = data.getParcelableExtra("data");
    }

回到桌面,HOME

Intent intent = new Intent();
                intent.setAction("android.intent.action.MAIN");
                intent.addCategory("android.intent.category.HOME");
                startActivity(intent);

从联系人选择电话号码

Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
                startActivityForResult(intent,0);

然后再

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Uri uri = data.getData();
        String[] names = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
        Cursor cursor = getContentResolver().query(uri, names,null, null, null);
        int index = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        String number = cursor.getString(index);
        Log.i(TAG, "onActivityResult: "+number);
    }

插入新的联系人,跳到新建联系人页面并自动填好信息。

                Intent intent = new Intent(Intent.ACTION_INSERT);
                intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
                intent.putExtra(ContactsContract.Intents.Insert.NAME, "aaa");
                intent.putExtra(ContactsContract.Intents.Insert.EMAIL, "wandjnfdjkn");
                intent.putExtra(ContactsContract.Intents.Insert.PHONE, "100200100");
                startActivity(intent);

安装apk文件

    String fileName = Environment.getExternalStorageDirectory() + "/a.apk" ;
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
    startActivity(intent);

启动QQ,并打开指定的聊天窗口

String url = "mqqwpa://im/chat?chat_type=wpa&uin=1492571688";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

打开手机上的地图软件,如百度地图。传入的坐标如果在范围内会直接点位到所传的坐标

Uri uri = Uri.parse("geo:40.2268400000,88.1141060000");
                Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                startActivity(intent);

打开系统各各个设置页面

如WiFi

Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivity(intent);

其他如

ACTION_SETTINGS
ACTION_WIRELESS_SETTINGS
ACTION_AIRPLANE_MODE_SETTINGS
ACTION_WIFI_SETTINGS
ACTION_APN_SETTINGS
ACTION_BLUETOOTH_SETTINGS
ACTION_DATE_SETTINGS
ACTION_LOCALE_SETTINGS
ACTION_INPUT_METHOD_SETTINGS
ACTION_DISPLAY_SETTINGS
ACTION_SECURITY_SETTINGS
ACTION_LOCATION_SOURCE_SETTINGS
ACTION_INTERNAL_STORAGE_SETTINGS
ACTION_MEMORY_CARD_SETTINGS

用浏览器打开网页

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

打开录音机

Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivity(intent);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值