Intents and Intent Filters用法


Example explicit intent

An explicit intent is one that you use to launch a specific app component, such asa particular activity or service in your app. To create an explicit intent, definethe component name for the Intent object—allother intent properties are optional.

For example, if you built a service in your app, named DownloadService,designed to download a file from the web, you can start it with the following code:

// Executed in an Activity, so 'this' is the Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData(Uri.parse(fileUrl));
startService(downloadIntent);

The Intent(Context, Class)constructor supplies the app Context and thecomponent a Class object. As such,this intent explicitly starts the DownloadService class in the app.

For more information about building and starting a service, see theServices guide.

Example implicit intent

An implicit intent specifies an action that can invoke any app on the device ableto perform the action. Using an implicit intent is useful when your app cannot perform theaction, but other apps probably can and you'd like the user to pick which app to use.

For example, if you have content you want the user to share with other people, create an intentwith the ACTION_SEND actionand add extras that specify the content to share. When you callstartActivity() with that intent, the user canpick an app through which to share the content.

Caution: It's possible that a user won't have anyapps that handle the implicit intent you send to startActivity(). If that happens, the call will fail and your app will crash. To verifythat an activity will receive the intent, call resolveActivity() on your Intent object. If the result is non-null,then there is at least one app that can handle the intent and it's safe to callstartActivity(). If the result is null,you should not use the intent and, if possible, you should disable the feature that issuesthe intent.

// Create the text message with a string
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // "text/plain" MIME type

// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent);
}

Note: In this case, a URI is not used, but the intent's data typeis declared to specify the content carried by the extras.

When startActivity() is called, the systemexamines all of the installed apps to determine which ones can handle this kind of intent (anintent with the ACTION_SEND action and that carries "text/plain"data). If there's only one app that can handle it, that app opens immediately and is given theintent. If multiple activities accept the intent, the systemdisplays a dialog so the user can pick which app to use..

参考:http://wear.techbrood.com/guide/components/intents-filters.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值