Android文档学习03_Intent

解析xml资源时,Android会忽略掉当前设备不支持的xml的属性。这样的话,在你的app中,你就可以放心的加入那些只有新系统才会支持的xml属性,而不用担心旧系统遇到这些代码时会出现错误。


让你的activity看起来像一个对话框:
 
<activity android:theme="@android:style/Theme.Dialog">
 
让你的activity拥有一个透明的背景:
 
<activity android:theme="@android:style/Theme.Translucent">
 
申请你要定制的主题,会在/res/values/styles.xml中定义:
 
<activity android:theme="@style/CustomTheme">
 
申请一个应用到你的app中所有activities的主题,在<application>标签中添加android:theme属性:
 
<application android:theme="@style/CustomTheme">
 


隐式的Intent


创建一个Intent来打电话,在这里我们用Uri数据来表示电话号码。
 
Uri number = Uri.parse("tel:5551234");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
 
当你的应用通过startActivity来使用该intent时,电话应用程序就会朝给定的号码打电话。
以下是一些其它的intent以及他们的action和Uri数据对:
访问一个地图:
 
// 基于地址的地图上的点
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
//或者基于经纬度
// Uri location = Uri.parse("geo:37.422219,-122.08364?z=14"); // z参数为缩放级别
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
 
访问一个网页:
 
Uri webpage = Uri.parse("http://www.android.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
 
其它的隐式intent需要"附加的"其它类型的数据,比如string,你可以通过不同的putExtra()方法来增加一个或多个附加数据。
默认情况下,系统通过intent附加的Uri数据来决定适合的MIME类型,如果你的intent里并没有Uri,那么你需要使用setType()来明确intent相关的数据类型。而确定MIME类型更加明确了哪一类activity才是最应该接受该intent的。
以下是包含附加数据的intent:
发送带附件的email:
 
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// 这个intent不含URI, 所以声明了"text/plain" MIME类型
emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"jon@example.com"}); // 收件人
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"));
//你也可以通过传递一组Uri来附加多个数据
 
创建一个月历事件。
 
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");
 






如果你发起一个在设备上无法被任何应用程序处理的intent,那么你的程序就会崩溃。


为了确认有一个activity能响应该intent,可以使用queryIntentActivities()来获取一个能处理该intent的activity列表,如果返回的列表非空,那么你就可以安全的使用这个intent了。例如:
 
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
 
如果 isIntentSafe为真,那么至少有一个程序会响应该intent,反之,则没有。
你应该在程序一开始时就进行该项检查,从而在用户尝试使用该intent之前移除这个功能。如果你知道一个特定的应用程序可以处理这个intent,那么你也可以提共一个链接来让用户下载该应用程序
以下是一个完整的例子,它向你展示了如何创建一个可以访问地图的的intent。先确定有一个可以处理该intent的程序,再启动它。
 
//创建intent
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
// 确定它可以被处理
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(mapIntent, 0);
boolean isIntentSafe = activities.size() > 0;
 
// 如果安全的话,启动一个activity
if (isIntentSafe) {
    startActivity(mapIntent);
}
 




为了显示选择器,调用createChooser()创建一个intent,并将其传给startActivity().比如:
 
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().getText(R.string.chooser_title);
// 创建并打开一个选择器
Intent chooser = Intent.createChooser(intent, title);
startActivity(chooser);
 
以上把intent传入createChooser()方法,从而显示了一个应用程序选择对话框,并将提供的文字作为对话框标题。




onActivityResult()方法
为了成功地处理结果,你必须明白作为结果返回的Intent将是什么格式 。当返回结果的Activity是自己所写的Activity时,这些确认是很容易的。Android平台里的应用程序提供了自己的APIs,你可以依赖其具体的结果数据。例如,联系人程序始终返回选定联系人的内容URI标识,相机应用程序返回一个位图。
 
   @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
        } 
    }
   }
 




读取联系人数据
上面这段展示如何从联系人应用中获得返回结果的代码并没有涉及到具体的获得细节,因为那里需要一些有关content providers的高级知识。 但是,如果你仍然想钻研细节,可以参考下面的代码,它展示了如何从返回结果中得到手机号。
 
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request it is that we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // Get the URI that points to the selected contact
            Uri contactUri = data.getData();
            // We only need the NUMBER column, because there will be only one row in the result
            String[] projection = {Phone.NUMBER};
            // Perform the query on the contact to get the NUMBER column
            // We don't need a selection or sort order (there's only one result for the given URI)
            // CAUTION: The query() method should be called from a separate thread to avoid blocking
            // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
            // Consider using CursorLoader to perform the query.
            Cursor cursor = getContentResolver()
                    .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();
            // Retrieve the phone number from the NUMBER column
            int column = cursor.getColumnIndex(Phone.NUMBER);
            String number = cursor.getString(column);
            // Do something with the phone number...
        }
    }
   }
 
注意: 在Android 2.3 (API level 9)之前,执行Contacts Provider上的查询 (例如上面代码的功能),需要你的应用声明了READ_CONTACTS权限 (详见 Security and Permissions)。但是,从Android 2.3开始,联系人信息在返回结果时可以授予你暂时的读取权限,但这个临时权限仅适用于所请求的特定联系人,除非你声明READ_CONTACTS权限,否则不能查询一个在那个intent Uri定义之外的联系人信息。



允许其他程序启动你的活动,你需要在你清单文件的相应<activity>元素中添加一个<intent-filter>元素。

为了准确确定你的意图活动可以操作,每个意图意图过滤器的添加应尽可能具体行动类型的条款活动所接受的数据


有一个活动的意图过滤器,在数据类型是文本或图像时,它能处理Action-Send意图

<activity android:name="ShareActivity">
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
        <data android:mimeType="image/*"/>
    </intent-filter>
</activity>








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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值