博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Sending the User to Another App
阅读量:4212 次
发布时间:2019-05-26

本文共 2643 字,大约阅读时间需要 8 分钟。

Android 最重要的特性就是app 可以发送数据让另外一个app来运行。假如你的app有旅行的数据需要显示在地图上,这时候没必要在app中建立一个地图。你可以用一个隐式Intent来新建一个request。Android 系统就开启动一个可以在地图上显示地址的app.
那如何建立一个隐式的Intent呢?
隐式的Intent 不需要指定准确的名字,但是需要指定一个action且一般都带有要处理的数据.来提示你需要的动作,如看图片,编辑等。
下面是一个隐式的Intent来打电话。
Uri number = Uri.parse("tel:5551234");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
看图片:
// 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);
看网页:
Uri webpage = Uri.parse("http://www.android.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
你可以用putExtra来添加额外的数据.用setType来指定携带数据的类型.
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
在发出这类隐式Intent之前,一般要保证有特性的activity来处理此类action。一般通过查询packageManager的queryIntentActivities来查询是否有符合的activity 
如下所示:
PackageManager packageManager = getPackageManager();
List activities = packageManager.queryIntentActivities(intent,
        PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafe = activities.size() > 0;
一个完成的例子如下:
// Build the intent
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
// Verify it resolves
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(mapIntent, 0);
boolean isIntentSafe = activities.size() > 0;
// Start an activity if it's safe
if (isIntentSafe) {
    startActivity(mapIntent);
}
但是如果有多余一个activity 可以出来此类action时,一般会加一个chooser供用户选择。如下所示:
Intent intent = new Intent(Intent.ACTION_SEND);
...
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(chooser);
}

转载地址:http://svcmi.baihongyu.com/

你可能感兴趣的文章
图的割点(简易邻接矩阵)C~
查看>>
两个变量实现交换
查看>>
求割点(邻接表无向图)C~
查看>>
求图的割边(桥)(邻接矩阵 无向图)C~
查看>>
快速排序(quick sort) C ~
查看>>
二叉树的遍历(先、中、后、层序)C实现
查看>>
判断是否是同一颗二叉树
查看>>
C语言文件操作
查看>>
简易的多组数据题模板
查看>>
解决负权边的算法(Bellman Ford )(有向图) (1)C ~
查看>>
循环链表实现约瑟夫环(C实现)~
查看>>
用数组模拟链表操作 C实现~
查看>>
Bellman Ford 的队列优化 (2) C~
查看>>
子序列和
查看>>
表排序(基于插入排序) C~
查看>>
C 计时器大全
查看>>
简易贪吃蛇 C ~
查看>>
C 语言 printf 用法
查看>>
排列(暴力穷举)
查看>>
蛇形填数
查看>>