实现自己与其他app交互

一,不同app间交互,使用intent实现。

下面是些常见的intent;

<span style="font-size:18px;">1.拨打电话,
	Uri number = Uri.parse("tel:5551234");
	Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
2.地图查看,
		// Map point based on address
	Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
	// Or map point based on latitude/longitude
	// Uri location = Uri.parse("geo:37.422219,-122.08364?z=14"); // z param is zoom level
	Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
3.打开网页,
	Uri webpage = Uri.parse("http://www.android.com");
	Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
 
 4.发送Email,
	 Intent emailIntent = new Intent(Intent.ACTION_SEND);
	// The intent does not have a URI, so declare the "text/plain" MIME type
	emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
	emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"jon@example.com"}); // recipients
	emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
	emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text");
	emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment"));
	// You can also attach multiple items by passing an ArrayList of Uris

 5.创建日历事物,
	Intent calendarIntent = new Intent(Intent.ACTION_INSERT, Events.CONTENT_URI);
	Calendar beginTime = Calendar.getInstance().set(2012, 0, 19, 7, 30);
	Calendar endTime = Calendar.getInstance().set(2012, 0, 19, 10, 30);
	calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
	calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
	calendarIntent.putExtra(Events.TITLE, "Ninja class");
	calendarIntent.putExtra(Events.EVENT_LOCATION, "Secret dojo");</span>
尽管上面这些在android系统上定会有相应的app来接收该intent,但是为了确保,我们应该加些判断
 来判断确实有app来响应intent;

<span style="font-size:18px;">PackageManager packageManager = getPackageManager();
	List activities = packageManager.queryIntentActivities(intent,
	        PackageManager.MATCH_DEFAULT_ONLY);
	boolean isIntentSafe = activities.size() > 0;
 	// Start an activity if it's safe
	if (isIntentSafe) {
	    startActivity(mapIntent);
	}</span>

如果有多个应用能够接收该intent那么会弹出对话框让用户选择,这里你可以使用
Intent chooser = Intent.createChooser(intent, title);
为该对话框起一个标题

<span style="font-size:18px;">二,接收intent的返回值,
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}
三,也可以为自己创建的app分享某一个功能供其他app调用。这要在对应的activity的属性上
添加<intent-filter></span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值