通过Intent跳转到系统应用

本文提供了丰富的 Android Intent 使用案例,涵盖拨打电话、发送短信、启动应用、打开摄像头等常见操作,帮助开发者快速掌握 Intent 的应用场景。
拨号界面,代码如下:
Intent intent =new Intent(); 
 
            intent.setAction("android.intent.action.CALL_BUTTON");  
 
            startActivity(intent);
复制代码

Uri uri = Uri.parse("tel:xxxxxx");   
 
Intent intent = new Intent(Intent.ACTION_DIAL, uri);     
 
startActivity(intent);  
复制代码
两者都行   
但是如果是跳转到应用,使用一下代码:
Intent intent= new Intent("android.intent.action.DIAL");   
 
intent.setClassName("com.android.contacts","com.android.contacts.DialtactsActivity");
复制代码
到通话记录界面:
Intent intent=new Intent();  
 
intent.setAction(Intent.ACTION_CALL_BUTTON);  
 
startActivity(intent);  
复制代码
到联系人界面:
Intent intent = new Intent();   
 
intent.setAction(Intent.ACTION_VIEW);   
 
intent.setData(Contacts.People.CONTENT_URI);   
 
startActivity(intent);
复制代码
同理,到应用:
Intent intent= new Intent("com.android.contacts.action.LIST_STREQUENT");   
 
intent.setClassName("com.android.contacts","com.android.contacts.DialtactsActivity");
复制代码
调用联系人界面:
Intent intent = new Intent();   
 
intent.setAction(Intent.ACTION_PICK);   
 
intent.setData(Contacts.People.CONTENT_URI);   
 
startActivity(intent);   
复制代码
插入联系人
Intent intent=new Intent(Intent.ACTION_EDIT,Uri.parse("content://com.android.contacts/contacts/"+"1"));  
 
startActivity(intent);  
复制代码
到联系人列表界面   
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);  
 
            intent.setType("vnd.android.cursor.item/person");  
 
            intent.setType("vnd.android.cursor.item/contact");  
 
            intent.setType("vnd.android.cursor.item/raw_contact");  
 
            intent.putExtra(android.provider.ContactsContract.Intents.Insert.NAME, name);  
 
            intent.putExtra(android.provider.ContactsContract.Intents.Insert.COMPANY,company);  
 
            intent.putExtra(android.provider.ContactsContract.Intents.Insert.PHONE, tel);  
 
            intent.putExtra(android.provider.ContactsContract.Intents.Insert.PHONE_TYPE, 3);
复制代码
到短信界面:
Intent intent = new Intent(Intent.ACTION_VIEW);  
 
                intent.setType("vnd.android-dir/mms-sms");  
 
//              intent.setData(Uri.parse("content://mms-sms/conversations/"));//此为号码  
 
                startActivity(intent);  
复制代码
到应用:
Intent intent = new Intent("android.intent.action.CONVERSATION");  
 
startActivity(intent);  
复制代码
以下是在网上找到的其他方法:




1.从google搜索内容
Intent intent = new Intent();   
 
intent.setAction(Intent.ACTION_WEB_SEARCH);   
 
intent.putExtra(SearchManager.QUERY,"searchString")   
 
startActivity(intent);   
复制代码
2.浏览网页
Uri uri = Uri.parse("http://www.google.com");   
 
Intent it   = new Intent(Intent.ACTION_VIEW,uri);   
 
startActivity(it);
复制代码
3.显示地图
Uri uri = Uri.parse("geo:38.899533,-77.036476");   
 
Intent it = new Intent(Intent.Action_VIEW,uri);   
 
startActivity(it);   
复制代码
4.路径规划
Uri uri = Uri.parse("http://maps.google.com/maps?f=dsaddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");   
 
Intent it = new Intent(Intent.ACTION_VIEW,URI);   
 
startActivity(it);  
复制代码
5.拨打电话
Uri uri = Uri.parse("tel:xxxxxx");   
 
Intent it = new Intent(Intent.ACTION_DIAL, uri);     
 
startActivity(it);  
复制代码

uri = Uri.parse("tel:"+number);    
 
                intent = new Intent(Intent.ACTION_CALL,uri);    
 
                startActivity(intent);  
复制代码
其中不同自己试验一下就知道了。 


6.调用发短信的程序
Intent it = new Intent(Intent.ACTION_VIEW);     
 
it.putExtra("sms_body", "The SMS text");     
 
it.setType("vnd.android-dir/mms-sms");     
 
startActivity(it);
复制代码

uri = Uri.parse("smsto:"+要发送短信的对方的number);    
 
                intent = new Intent(Intent.ACTION_SENDTO,uri);    
 
                startActivity(intent);   
复制代码

mIntent = new Intent(Intent.ACTION_VIEW);    
 
        mIntent.putExtra("address", c.getString(c.getColumnIndex(column)));    
 
        mIntent.setType("vnd.android-dir/mms-sms");    
 
        startActivity(mIntent);   
复制代码
7.发送短信
Uri uri = Uri.parse("smsto:0800000123");     
 
Intent it = new Intent(Intent.ACTION_SENDTO, uri);     
 
it.putExtra("sms_body", "The SMS text");     
 
startActivity(it);   
 
String body="this is sms demo";   
 
Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", number, null));   
 
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);   
 
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);   
 
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);   
 
startActivity(mmsintent);<span style="font-family:Simsun;white-space: normal; background-color: rgb(255, 255, 255);"> </span>  
复制代码
8.发送彩信
Uri uri = Uri.parse("content://media/external/images/media/23");     
 
Intent it = new Intent(Intent.ACTION_SEND);     
 
it.putExtra("sms_body", "some text");     
 
it.putExtra(Intent.EXTRA_STREAM, uri);     
 
it.setType("image/png");     
 
startActivity(it);   
 
StringBuilder sb = new StringBuilder();   
 
sb.append("file://");   
 
sb.append(fd.getAbsoluteFile());   
 
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mmsto", number, null));   
 
// Below extra datas are all optional.   
 
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject);   
 
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);   
 
intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString());   
 
intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode);   
 
intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent);   
 
startActivity(intent);   
复制代码
9.发送Email
Uri uri = Uri.parse("mailto:xxx@abc.com");   
 
Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
 
startActivity(it);   
 
Intent it = new Intent(Intent.ACTION_SEND);     
 
it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");     
 
it.putExtra(Intent.EXTRA_TEXT, "The email body text");     
 
it.setType("text/plain");     
 
startActivity(Intent.createChooser(it, "Choose Email Client"));   
 
Intent it=new Intent(Intent.ACTION_SEND);       
 
String[] tos={"me@abc.com"};       
 
String[] ccs={"you@abc.com"};       
 
it.putExtra(Intent.EXTRA_EMAIL, tos);       
 
it.putExtra(Intent.EXTRA_CC, ccs);       
 
it.putExtra(Intent.EXTRA_TEXT, "The email body text");       
 
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");       
 
it.setType("message/rfc822");       
 
startActivity(Intent.createChooser(it, "Choose Email Client"));     
 
  
 
Intent it = new Intent(Intent.ACTION_SEND);     
 
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");     
 
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");     
 
sendIntent.setType("audio/mp3");     
 
startActivity(Intent.createChooser(it, "Choose Email Client"));   
复制代码
10.播放多媒体   
Intent it = new Intent(Intent.ACTION_VIEW);   
 
Uri uri = Uri.parse("file:///sdcard/song.mp3");   
 
it.setDataAndType(uri, "audio/mp3");   
 
startActivity(it);   
 
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");     
 
Intent it = new Intent(Intent.ACTION_VIEW, uri);     
 
startActivity(it);   
复制代码
11.uninstall apk
Uri uri = Uri.fromParts("package", strPackageName, null);     
 
Intent it = new Intent(Intent.ACTION_DELETE, uri);     
 
startActivity(it);  
复制代码
12.install apk
Uri installUri = Uri.fromParts("package", "xxx", null);   
 
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);  
复制代码
13. 打开照相机
<1>Intent i = new Intent(Intent.ACTION_CAMERA_BUTTON, null);   
 
          this.sendBroadcast(i);   
 
    <2>long dateTaken = System.currentTimeMillis();   
 
         String name = createName(dateTaken) + ".jpg";   
 
         fileName = folder + name;   
 
         ContentValues values = new ContentValues();   
 
         values.put(Images.Media.TITLE, fileName);   
 
         values.put("_data", fileName);   
 
         values.put(Images.Media.PICASA_ID, fileName);   
 
         values.put(Images.Media.DISPLAY_NAME, fileName);   
 
         values.put(Images.Media.DESCRIPTION, fileName);   
 
         values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, fileName);   
 
         Uri photoUri = getContentResolver().insert(   
 
                   MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);   
 
             
 
         Intent inttPhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
 
         inttPhoto.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);   
 
         startActivityForResult(inttPhoto, 10);
复制代码
14.从gallery选取图片
Intent i = new Intent();   
 
        i.setType("image/*");   
 
        i.setAction(Intent.ACTION_GET_CONTENT);   
 
        startActivityForResult(i, 11);   
复制代码
15. 打开录音机
Intent mi = new Intent(Media.RECORD_SOUND_ACTION);   
 
         startActivity(mi);
复制代码
16.显示应用详细列表
Uri uri = Uri.parse("market://details?id=app_id");          
 
Intent it = new Intent(Intent.ACTION_VIEW, uri);          
 
startActivity(it);          
 
//where app_id is the application ID, find the ID           
 
//by clicking on your application on Market home           
 
//page, and notice the ID from the address bar<span style="font-family:Simsun;white-space: normal; background-color: rgb(255, 255, 255);">    </span>
复制代码
刚才找app id未果,结果发现用package name也可以 Uri uri = Uri.parse("market://details?id=<packagename>");
这个简单多了 


17寻找应用     
Uri uri = Uri.parse("market://search?q=pname:pkg_name");          
 
Intent it = new Intent(Intent.ACTION_VIEW, uri);          
 
startActivity(it);   
 
//where pkg_name is the full package path for an application<span style="font-family:Simsun;white-space: normal; background-color: rgb(255, 255, 255);">     </span>  
复制代码
18打开联系人列表
Intent i = new Intent();   
 
         i.setAction(Intent.ACTION_GET_CONTENT);   
 
         i.setType("vnd.android.cursor.item/phone");   
 
         startActivityForResult(i, REQUEST_TEXT);
复制代码
Uri uri = Uri.parse("content://contacts/people");   
 
         Intent it = new Intent(Intent.ACTION_PICK, uri);   
 
         startActivityForResult(it, REQUEST_TEXT);  
复制代码
19 打开另一程序
Intent i = new Intent();   
 
         ComponentName cn = new ComponentName("com.yellowbook.android2",   
 
                   "com.yellowbook.android2.AndroidSearch");   
 
         i.setComponent(cn);   
 
         i.setAction("android.intent.action.MAIN");   
 
         startActivityForResult(i, RESULT_OK);  
复制代码
20 添加到短信收件箱
ContentValues cv = new ContentValues();      
 
                cv.put("type", "1");   
 
cv.put("address","短信地址");  
 
cv.put("body", "短信内容");   
 
getContentResolver().insert(Uri.parse("content://sms/inbox"), cv);
复制代码
21 从sim卡或者联系人中查询
Cursor cursor;  
 
        Uri uri;  
 
        if (type == 1) {  
 
            Intent intent = new Intent();  
 
            intent.setData(Uri.parse("content://icc/adn"));  
 
            uri = intent.getData();  
 
        } else  
 
            uri = People.CONTENT_URI;  
 
  
 
        cursor = activity.getContentResolver().query(uri, null, null, null, null);  
 
while (cursor.moveToNext()) {  
 
int peopleId = cursor.getColumnIndex(People._ID);
 
int nameId = cursor.getColumnIndex(People.NAME); 
 
int phoneId = cursor.getColumnIndex(People.NUMBER);}
复制代码
查看某个联系人,当然这里是ACTION_VIEW,如果为选择并返回action改为ACTION_PICK,当然处理intent时返回需要用到 startActivityforResult 
Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, ID);//最后的ID参数为联系人Provider中的数据库BaseID,即哪一行 
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(personUri); startActivity(intent); 






 
22 删除
uri = ContentUris.withAppendedId(People.CONTENT_URI, 联系人id);  
 
        int count = activity.getContentResolver().delete(uri, null, null
复制代码
23 添加到联系人:
ContentValues cv = new ContentValues();  
 
                    ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();  
 
                    ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);  
 
                    builder.withValues(cv);  
 
                    operationList.add(builder.build());  
 
                    builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);  
 
                    builder.withValueBackReference(StructuredName.RAW_CONTACT_ID, 0);  
 
                    builder.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);  
 
                    builder.withValue(StructuredName.DISPLAY_NAME, "自定义联系人名");  
 
                    operationList.add(builder.build());  
 
                    builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);  
 
                    builder.withValueBackReference(Phone.RAW_CONTACT_ID, 0);  
 
                    builder.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);  
 
                    builder.withValue(Phone.NUMBER, "联系人的phonenumber");  
 
                    builder.withValue(Data.IS_PRIMARY, 1);  
 
                    operationList.add(builder.build());  
 
                    try {  
 
                        getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);  
 
                    } catch (RemoteException e) {  
 
                        e.printStackTrace();  
 
                    } catch (OperationApplicationException e) {  
 
                        e.printStackTrace();  
 
                    }  
复制代码
23 选择一个图片
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);   
 
intent.addCategory(Intent.CATEGORY_OPENABLE);  
 
intent.setType("image/*");  
 
startActivityForResult(intent, 0);   
复制代码
24 调用
Android
设备的照相机,并设置拍照后存放位置
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
 
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment .getExternalStorageDirectory().getAbsolutePath()+"/cwj", android123 + ".jpg"))); //存放位置为sdcard卡上cwj文件夹,文件名为android123.jpg格式  
 
startActivityForResult(intent, 0);  
复制代码
25 在market上搜索指定package name,比如搜索com.android123.cwj的写法如下
Uri uri = Uri.parse("market://search?q=pname:com.android123.cwj");  
 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
复制代码
26获取文件信息,并使用相对应软件打开
private void openFile(File f)    
 
{    
 
   Intent intent = new Intent();    
 
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
 
   intent.setAction(android.content.Intent.ACTION_VIEW);    
 
   String type = getMIMEType(f);    
 
   intent.setDataAndType(Uri.fromFile(f), type);    
 
   startActivity(intent);    
 
}    
 
   
 
private String getMIMEType(File f){    
 
   String end = f    
 
       .getName()    
 
       .substring(f.getName().lastIndexOf(".") + 1,    
 
           f.getName().length()).toLowerCase();    
 
   String type = "";    
 
   if (end.equals("mp3") || end.equals("aac") || end.equals("aac")    
 
       || end.equals("amr") || end.equals("mpeg")    
 
       || end.equals("mp4"))    
 
   {    
 
     type = "audio";    
 
   } else if (end.equals("jpg") || end.equals("gif")    
 
       || end.equals("png") || end.equals("jpeg"))    
 
   {    
 
     type = "image";    
 
   } else    
 
   {    
 
     type = "*";    
 
   }    
 
   type += "/*";    
 
   return type;    
 
}
<think>我们正在讨论Android中通过Intent执行特定Action跳转。根据引用[1]和引用[2],我们可以知道Intent可以通过指定Action字符串来启动相应的活动。在Android中,我们可以使用显式Intent和隐式Intent。显式Intent直接指定目标组件的类名,而隐式Intent则通过指定Action、Category、Data等,由系统匹配可以处理该Intent的组件。用户现在想了解如何通过Intent执行特定Action跳转,这通常指的是隐式Intent的使用。因为特定Action跳转意味着我们并不指定具体的组件(Activity),而是指定一个动作(比如打开网页、发送邮件等),然后由系统中安装了能够处理该动作的应用来响应。 根据引用[2],我们看到了几种构造Intent的方法,其中有一个构造函数是`public Intent(Stringaction,Uri uri)`,这可以用于隐式Intent。此外,我们还可以使用`setAction`方法来设置Action。 下面我将详细介绍如何使用隐式Intent实现特定Action跳转。###使用隐式Intent实现特定Action跳转1.**创建一个Intent对象,并指定Action字符串**```javaIntentintent =new Intent();intent.setAction(Intent.ACTION_VIEW);//这里以查看网页为例,ACTION_VIEW是系统定义的一个动作```2.**设置数据(如果需要)**对于某些Action,需要提供数据。例如,打开网页需要提供网址:```javaintent.setData(Uri.parse("https://www.example.com"));```3.**启动Activity**```javastartActivity(intent);```###示例:打开网页```javaIntentintent =new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com"));startActivity(intent); ```###示例:发送邮件```javaIntent intent= newIntent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("mailto:contact@example.com"));//仅邮件应用处理intent.putExtra(Intent.EXTRA_SUBJECT,"邮件主题");intent.putExtra(Intent.EXTRA_TEXT, "邮件内容"); startActivity(intent);``` ###注意事项-使用隐式Intent时,系统可能会弹出选择器,让用户选择用哪个应用来执行该动作。-在调用`startActivity`之前,最好检查一下是否有应用可以处理这个Intent,以避免应用崩溃:```javaif (intent.resolveActivity(getPackageManager())!= null) {startActivity(intent);}else {//没有应用可以处理,给出提示}```###自定义Action除了使用系统定义的Action,我们也可以自定义Action。这通常用于自己应用内的组件通信。例如,我们可以在一个Activity中定义Action,然后在另一个Activity中声明相同的Action来响应。 1. **在AndroidManifest.xml中声明Activity时,指定intent-filter**```xml<activity android:name=".MyActivity"><intent-filter><actionandroid:name="com.example.myapp.MY_ACTION"/><categoryandroid:name="android.intent.category.DEFAULT" /></intent-filter></activity>```2.**启动这个Activity**```javaIntentintent =new Intent("com.example.myapp.MY_ACTION");startActivity(intent);```###总结通过Intent执行特定Action跳转,主要是使用隐式Intent,设置相应的Action,必要时设置数据和附加信息。系统会匹配能够处理该Intent的Activity(可能是本应用的,也可能是其他应用的)并启动。 §§相关问题§§1.如何防止使用隐式Intent时出现多个应用可供选择的情况? 2.如何为自定义ActionIntent传递复杂数据?3.除了`startActivity`,还有哪些方法可以启动Activity?</think>### 通过Intent执行特定Action跳转的方法 在Android开发中,使用Intent执行特定Action跳转主要通过**隐式Intent**实现,核心是设置`Intent.ACTION_*`常量或自定义Action字符串。以下是具体方法: #### 1. **使用系统预定义Action** ```java // 示例1:打开网页 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("https://www.example.com")); startActivity(intent); // 示例2:发送邮件 Intent emailIntent = new Intent(Intent.ACTION_SENDTO); emailIntent.setData(Uri.parse("mailto:contact@example.com")); startActivity(emailIntent); ``` #### 2. **自定义Action跳转** 需在`AndroidManifest.xml`中声明目标Activity的`<intent-filter>`: ```xml <activity android:name=".CustomActionActivity"> <intent-filter> <action android:name="com.example.MY_CUSTOM_ACTION" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> ``` Java代码触发: ```java Intent intent = new Intent("com.example.MY_CUSTOM_ACTION"); startActivity(intent); ``` #### 3. **带附加数据的Action跳转** ```java Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT, "分享内容"); startActivity(Intent.createChooser(intent, "选择应用")); // 显示选择器 ``` #### 4. **通过ComponentName精确指定** ```java ComponentName comp = new ComponentName( "com.example.otherapp", "com.example.otherapp.TargetActivity" ); Intent intent = new Intent(); intent.setComponent(comp); intent.setAction(Intent.ACTION_VIEW); // 可选 startActivity(intent); ``` #### 关键注意事项: 1. **权限声明**:跨应用跳转需在`AndroidManifest.xml`声明`<queries>`或`<uses-permission>`[^1] 2. **空指针处理**:检查目标Activity是否存在 ```java if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } ``` 3. **常用系统Action**: - `Intent.ACTION_DIAL`:拨号界面 - `Intent.ACTION_SETTINGS`:系统设置 - `Intent.ACTION_GET_CONTENT`:获取文件 --- ### 相关问题 1. 如何防止隐式Intent被未授权应用响应? 2. 自定义Action跳转时如何传递复杂数据对象? 3. 系统预定义的常用Intent Action有哪些实际应用场景? 4. 如何通过Intent Filter实现多个Activity响应同一Action? [^1]: 引用自AndroidManifest.xml配置示例 [^2]: 参考Intent构造函数及方法说明
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值