Android 用Intent调用系统中经常被用到的组件

调用系统音乐播放器

1 Intent internt = new Intent(Intent.ACTION_VIEW);
2 internt.setDataAndType(Uri.parse(item.url), "audio/mp3");
3 internt.setComponent(new ComponentName("com.android.music","com.android.music.MediaPlaybackActivity"));
4 startActivity(internt);

调用系统视频播放器

1 Intent intent = new Intent(Intent.ACTION_VIEW);
2 intent .setDataAndType(Uri.parse("/sdcard/a.mp4"), "video/mp4");
3 startActivity(intent );

 


发送SMS/MMS
调用发送短信的程序

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 = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);

其二是使用SmsManager: 直接发送短信 

EditText num=(EditText)findViewById(R.id.num);

                EditText content=(EditText)findViewById(R.id.content);

                String mobile=num.getText().toString();

                String smstext=content.getText().toString();

                //获取SmsManager

                SmsManager sms=SmsManager.getDefault();

                //如果内容大于70字,则拆分为多条

                List<String> texts=sms.divideMessage(smstext);

                //逐条发送短信

                for(String text:texts)

                {

                    sms.sendTextMessage(mobile, null, text, null, null);

                }                

                //发送结果提示

                Toast.makeText(SendSMS.this, "发送成功", Toast.LENGTH_LONG).show();

 

关于SmsManager

SDK中的介绍:Manages SMS operations such as sending data, text, and pdu SMS messages. Get this object by calling the static method SmsManager.getDefault().

方法:

public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

 

destinationAddress: 收件人地址

scAddress: 短信中心号码,null为默认中心号码

sentIntent: 当消息发出时,成功或者失败的信息报告通过PendingIntent来广播。如果该参数为空,则发信程序会被所有位置程序检查一遍,这样会导致发送时间延长。

deliveryIntent: 当消息发送到收件人时,该PendingIntent会被广播。pdu数据在状态报告的extended data ("pdu")中。

如果收件人或者信息为空则抛出 IllegalArgumentException 。

public ArrayList<String> divideMessage (String text)

将大于70字的短信分割为多条。

参数:text    the original message. Must not be null.

返回:an ArrayList of strings that, in order, comprise the original message

sendDataMessage 参数与上类似,只是用于发送Data。

sendMultipartTextMessage发送多条短信,发送内容必须是用divideMessage分割好了的。

 分享软件

intent=new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:"+""));
intent.putExtra("sms_body", "我在安卓市场发现了个好东东【通讯录】,快来......下载吧!!安卓市场里面还有很多应用来看看吧!");
startActivity(intent); 

 

发送彩信

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

 

拨打电话

调拨打电话界面

Uri telUri = Uri. parse ( "tel:100861" );
returnIt = new Intent(Intent. ACTION_DIAL , telUri);

直接拨打电话

 Uri uri = Uri.parse("tel:xxxxxx");
Intent it =new Intent(Intent.ACTION_CALL,uri);

 要使用这个必须在配置文件中加入<uses-permission id="android.permission.CALL_PHONE" />


联系人
进入联系人页面

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW); 
intent.setData(People.CONTENT_URI);
startActivity(intent);

打开联系人列表 

//第一种
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);

 

查看指定联系人

Uri personUri = ContentUris.withAppendedId 
(People.CONTENT_URI, info.id);//info.id联系人ID 
Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_VIEW);
intent.setData(personUri);
startActivity(intent);

 

显示网页:

Uri uri = Uri.parse("http://www.google.com");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);

 

web浏览器

Uri uri= Uri. parse ( "http://kuikui.javaeye.com" );
returnIt = new Intent(Intent. ACTION_VIEW , uri );

 

显示地图:

Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.Action_VIEW,uri);
startActivity(it);

 

发邮件

returnIt = new Intent(Intent. ACTION_SEND );

String[] tos = { "[email protected]" };

String[] ccs = { "[email protected]" };

returnIt .putExtra(Intent. EXTRA_EMAIL , tos);

returnIt .putExtra(Intent. EXTRA_CC , ccs);

returnIt .putExtra(Intent. EXTRA_TEXT , "body" );

returnIt .putExtra(Intent. EXTRA_SUBJECT , "subject" );

returnIt .setType( "message/rfc882" );//真机上使用这行

//returnIt.setType("text/plain");//模拟器请使用这行
Intent. createChooser ( returnIt , "Choose Email 
Client" );

 

直接发邮件

Uri smsToUri = Uri. parse ( "smsto://100861" );
returnIt = new Intent(Intent. ACTION_SENDTO ,
smsToUri);
returnIt.putExtra( "sms_body" , "shenrenkui" );

发彩信

Ur i mmsUri = Uri. parse (
"content://media/external/images/media/23" );

returnIt = new Intent(Intent. ACTION_SEND );

returnIt.putExtra( "sms_body" , "shenrenkui" );

returnIt.putExtra(Intent. EXTRA_STREAM , mmsUri);

returnIt.setType( "image/png" )

添加附件

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

播放多媒体

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

路径规划:

Uri uri = Uri.parse("http://maps.google.com/maps?
f=d&saddr=startLat%20startLng&daddr=endLat
%20endLng&hl=en");
Intent it = new Intent(Intent.ACTION_VIEW,uri );
startActivity(it);
安装
Uri installUri = Uri. fromParts ( "package" , " xxx " ,
null );
returnIt = new Intent(Intent. ACTION_PACKAGE_ADDED ,
installUri);


Uninstall 程序

Uri uri = Uri.fromParts("package", strPackageName,
null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);

 

 

 

你可能感兴趣的:(android)