❶ android Intent 如何接收到指定的Intent传递过来的值呢
首先,尽量不要用try{}catch去捕捉能用判断规避的异常,那样会影响效率,每次出现异常,虚拟机要抓错误调用堆栈。所以,最好的方式是通过判断去规避。
按你的思路,可以先判断getIntent.getExtras()是否为null。
Intent
_getIntent
=
this.getIntent();
if(
_getIntent.getExtras()
!=
null){
Log.i("YuryLog","理论上只有点了确认键才执行");
receiveName
=
_getIntent.getExtras().getString("sendName");
receiveEatSomething
=
_getIntent.getExtras().getString("sendeatSomething");
receiveCopies
=
_getIntent.getExtras().getString("sendcopies");
......
要指出的是,上述代码,最好使用getXXXExtra这类方法,它不会出现空指针(除了少数几个,比方说getStringExtra)。
需要设定默认值的,在没有值时它会返回默认值;没有设置默认值的,在没有值时会返回null,针对这类判空一下。
可以看下getBooleanExtra的源码:
public
boolean
getBooleanExtra(String
name,
boolean
defaultValue)
{
return
mExtras
==
null
?
defaultValue
:
mExtras.getBoolean(name,
defaultValue);
}
而getExtras()在没有值时会返回null,看下源码:
public
Bundle
getExtras()
{
return
(mExtras
!=
null)
?
new
Bundle(mExtras)
:
null;
}
所以,最好不要用getIntent().getExtras()这种方式,换用getIntent().getXXXExtras(),这样针对有设置默认值的就不需要判空了。
activity之间传值,是没有机制可以确定哪个activity传过来的。这是考虑到代码的可扩展性,解耦。要确定哪个activity发过来,在intent创建那里多传个布尔值就行,比方说下面的代码。
发送
intent.putExtra("fromXXActivity",
true);
接收
if
(getIntent().getBooleanExtra("fromXXActivity",
false))
{
......
//
这里,你就可以安全的接收那个activity发过来的所有值。
}
❷ android中intent什么意思
英文里 Intent是“意向、打算”的意思,其实就是告诉别人你的意图的意思了,这么理解Android里面的Intent也就不难了。
书面化的解释是:
Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。
❸ 如何在Android7.0系统下通过Intent安装apk
Android系统升级到7.0之后,安全性提高了不少,过去我们通常是使用这样的代码进行apk的安装操作。
“`
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(apkFile), “application/vnd.android.package-archive”);
context.startActivity(intent);
“`
但是在Android7.0的系统上,运行这段代码,会报如下错误。
Caused by: android.os.FileUriExposedException
原因是,安卓官方为了提高私有文件的安全性,面向 Android 7.0 或更高版本的应用私有目录被限制访问(0700)。此设置可防止私有文件的元数据泄漏,如它们的大小或存在性.
传递软件包网域外的 file:// URI 可能给接收器留下无法访问的路径。因此,尝试传递 file:// URI 会触发 FileUriExposedException。分享私有文件内容的推荐方法是使用 FileProvider。
1.定义一个FileProvider
<manifest>
...
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
...
</provider>
...
</application>
</manifest>
2.添加可用权限的文件目录
在res目录下,增加xml文件夹,并新建一个名为 file_paths.xml 的文件。文件内容格式如下:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="name1" path="test1" />
...
</paths>
标签下面必须包含至少包含以下标签中的一个或者多个。
files-path
<files-path name="name1" path="test1" />
表示Context.getFilesDir()目录或者其子目录。
示例 : /data/data/com.chen.gradle/files/test1
cache-path
<cache-path name="name2" path="test2" />
表示Context.getCacheDir()目录或者其子目录。
示例 : /data/data/com.chen.gradle/cache/test2
external-path
<external-path name="name3" path="test3" />
表示Environment.getExternalStorageDirectory()目录或者其子目录。
示例 : /storage/emulated/0/test3
external-files-path
<external-files-path name="name4" path="test4" />
表示Context.getExternalFilesDir(null)目录或者其子目录。
示例 : /storage/emulated/0/Android/data/com.chen.gradle/files/test4
external-cache-path
<external-cache-path name="name5" path="test5" />
表示Context.getExternalCacheDir()目录或者其子目录。
示例 : /storage/emulated/0/Android/data/com.chen.gradle/cache/test5
3.增加到provider
通过<meta-data>标签将上面的filepath添加到provider当中。
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
4.通过provider生成Uri
File imagePath = new File(Context.getFilesDir(), "test1");
File newFile = new File(imagePath, "default_image.jpg");
Uri contentUri = FileProvider.getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);
5.赋予临时权限给Uri
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
最终安装apk的代码变成这样:
public static void installApk(Context context, String apkPath) {
if (context == null || TextUtils.isEmpty(apkPath)) {
return;
}
File file = new File(apkPath);
Intent intent = new Intent(Intent.ACTION_VIEW);
//判读版本是否在7.0以上
if (Build.VERSION.SDK_INT >= 24) {
//provider authorities
Uri apkUri = FileProvider.getUriForFile(context, "com.mydomain.fileprovider", file);
//Granting Temporary Permissions to a URI
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
context.startActivity(intent);
}
❹ android中Intent问题
Android 开发网站上解释了一下,如果你想允许其它的app通过 intent-filter 命中你的 app 启动它,我们需要给我们的 app 添加 default category,一般来说没有 default category 是表示这个 activity 肯定只是我们 app 自己使用,比如我们一个app有多个 activity,只有主控 activity 会访问其它的 activity 时就是这样的。因为隐含地启动一个 app 的方式是通过对比 intent 条件的,我们没有指定 default category 就是表示我们不打算被其它程序隐含地启动(比如我们想放个木马什么的)。
这个文档说明了,想以隐含方式启动 activity 就需要添加 default category,这是因为需要允许其它app来启动你的activity (而启动自己的activity甚至可以直接使用类名来,不需要这么麻烦)。
另外,当我们希望把主控activity列在应用程序列表中时我们就给它添加 launcher category。
举个例子,一个产品管理程序,主控activity是先打开当前热销产品列表,它在手机的应用程序列表中,因此需要一个 launcher category,它有一个产品详细介绍的activity可以允许通过一个产品编号来查看产品,甚至在网页上有个链接,这时这个产品详细介绍activity就需要一个default category但不需要launcher category,而另一个修改产品资料的activity则不是必须添加一个category,因为它只会被主控activity启动并且外部其它app不应该有机会隐含地启动它。
http://developer.android.com/guide/components/intents-filters.html
❺ android 怎么跳转到位置与安全那个页面...intent 的action和type分别是什么...指教...
action是要执行的动作的一个简要描述,如VIEW_ACTION(查看)、EDIT_ACTION(修改)等,Android为我们定义了一套标准动作:
MAIN_ACTION
VIEW_ACTION
EDIT_ACTION
PICK_ACTION
GET_CONTENT_ACTION
DIAL_ACTION
CALL_ACTION
SENDTO_ACTION
ANSWER_ACTION
INSERT_ACTION
DELETE_ACTION
RUN_ACTION
LOGIN_ACTION
CLEAR_CREDENTIALS_ACTION
SYNC_ACTION
PICK_ACTIVITY_ACTION
WEB_SEARCH_ACTION
此外,我们还可以根据应用的需要,定义我们自己的动作,并可定义相应的Activity来处理我们的自定义动作。
type(数据类型),显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。
在应用中,我们可以以两种形式来使用Intent:
直接Intent:指定了component属性的Intent(调用setComponent(ComponentName)或者setClass(Context, Class)来指定)。通过指定具体的组件类,通知应用启动对应的组件。
间接Intent:没有指定comonent属性的Intent。这些Intent需要包含足够的信息,这样系统才能根据这些信息,在在所有的可用组件中,确定满足此Intent的组件。
具体的你可以查查资料,希望这些对你有帮助
❻ android中intent的作用
意图和意图过滤器Intents and Intent Filters
一个应用程序的三个核心组件-活动,服务和广播接收器是通过消息即意图(Intents)来激活的。Intent息传送是相同或不同应用中组件运行时晚绑定的一种机制。意图本身,一个意图对象,是一个包含被执行操作抽象描述的被动的数据结构-或者,对于广播而言,是某件已经发生并被声明的事情的描述。存在不同的机制来传送意图到每种组件中:
• 一个意图对象是传递给Context.startActivity()或者Activity.startActivityForResult()来启动一个活动或者让一个存在的活动去做某些新的事情。
• 一个意图对象是传递给Context.startService()来发起一个服务或者递交新的指令给运行中的服务。类似的,一个意图能被传递给Context.bindService() 来在调用组件和一个目标服务之间建立连接。作为一个可选项,它可以发起这个服务如果还没运行的话。
• 传递给任意广播方法(例如Context.sendBroadcast(),Context.sendOrderedBroadcast(), 或者Context.sendStickyBroadcast())的意图对象被传递给所有感兴趣的广播接收者。许多种广播产生于系统代码。
在每个例子里,Android系统找到合适的活动,服务,或者一组广播接收者来回应这个意图,必要时实例化它们。这些消息传送系统没有重叠:广播意图仅被传递给广播接收者,永远不会给活动或者服务。一个传送给startActivity()的意图是只会被传递给一个活动,永远不会给一个服务或广播接收者,如此类推。
这篇文档以意图对象的描述开始,然后描述Android映射意图到组件的规则-如何解决哪个组件应该接收一个意图消息。对于没有显式命名一个目标组件的意图,这个过程包括对照与潜在目标相关联的意图过滤器来测试这个意图对象。
意图对象Intent Objects
一个意图Intent对象是一堆信息。它包含接收这个意图的组件感兴趣的信息(例如将要采取的动作和操作的数据)再加上Android系统感兴趣的信息(例如应该处理这个意图的组件类别和如何启动一个目标活动的指令):
组件名称Component name
应该处理这个意图的组件名字. 这个字段是一个ComponentName对象- 一个组合物:目标组件的完全合格的类名 (比如"com.example.project.app.FreneticActivity") 以及应用程序描述文件中设置的组件所在包的名字(比如, "com.example.project"). 这个组件名字的包部分和描述文件中设置的包名字不一定要匹配。
组件名字是可选的。如果被设置了,这个意图对象将被传递到指定的类。如果没有, Android使用另外的意图对象中的信息去定位一个合适的目标- 请看本文稍后描述的意图解析Intent Resolution。
组件名字通过如下方法:setComponent(),setClass(), 或者setClassName()设置并通过getComponent()读取。
❼ android 里面intent类干什么的
intent即意图
一:用来启动其他新的Activity。
二:作为传递数据和事件的桥梁。传递数据时的代码有两种:
第一种是:
Intent
intent
=
new
Intent(CurrentActivity.this
,
OtherActivity.class);
intent.putExtra(“data”
,
somedata);
第二种是新建一个Bundle,再把该Bundle加入intent,如:
Bundle
bundle
=
new
Bundle()
;
bundle.putString(“data”
,
somedata)
;
intent.putExtras(bundle)。
❽ android中intent的作用 越详细越好
1 Intent.ACTION_MAIN
String: android.intent.action.MAIN
标识Activity为一个程序的开始。比较常用。
Input:nothing
Output:nothing
例如:
1
2
3
4
5
6
也可以直接在程序中实现 Intent it = new Intent(原Activity.class,需跳转Activity.class);
2 Intent.Action_CALL
Stirng: android.intent.action.CALL
呼叫指定的电话号码。
Input:电话号码。数据格式为:tel:+phone number
Output:Nothing
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:1320010001");
startActivity(intent);
3 Intent.Action.DIAL
String: action.intent.action.DIAL
调用拨号面板
Intent intent=new Intent();
intent.setAction(Intent.ACTION_DIAL); //android.intent.action.DIAL
intent.setData(Uri.parse("tel:1320010001");
startActivity(intent);
Input:电话号码。数据格式为:tel:+phone number
Output:Nothing
说明:打开Android的拨号UI。如果没有设置数据,则打开一个空的UI,如果设置数据,action.DIAL则通过调用getData()获取电话号码。
但设置电话号码的数据格式为 tel:+phone number.
4.Intent.Action.ALL_APPS
String: andriod.intent.action.ALL_APPS
列出所有的应用。
Input:Nothing.
Output:Nothing.
5.Intent.ACTION_ANSWER
Stirng:android.intent.action.ANSWER
处理呼入的电话。
Input:Nothing.
Output:Nothing.
6 Intent.ACTION_ATTACH_DATA
String: android.action.ATTCH_DATA
别用于指定一些数据应该附属于一些其他的地方,例如,图片数据应该附属于联系人
Input: Data
Output:nothing
7 Intent.ACTION_BUG_REPORT
String: android.intent.action.BUG_REPORT
显示Dug报告。
Input:nothing
output:nothing
8 Intent.Action_CALL_BUTTON
String: android.action.intent.CALL_BUTTON.
相当于用户按下“拨号”键。经测试显示的是“通话记录”
Input:nothing
Output:nothing
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
startActivity(intent);
9 Intent.ACTION_CHOOSER
String: android.intent.action.CHOOSER
显示一个activity选择器,允许用户在进程之前选择他们想要的,与之对应的是Intent.ACTION_GET_CONTENT.
10. Intent.ACTION_GET_CONTENT
String: android.intent.action.GET_CONTENT
允许用户选择特殊种类的数据,并返回(特殊种类的数据:照一张相片或录一段音)
Input: Type
Output:URI
这个以前用到过,看事例。
选择一个图片:
代码
int requestCode = 1001;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); // "android.intent.action.GET_CONTENT"
intent.setType("image/*"); // 查看类型,如果是其他类型,比如视频则替换成 video/*,或 */*
Intent wrapperIntent = Intent.createChooser(intent, null);
startActivityForResult(wrapperIntent, requestCode);
❾ android中为什么要使用intent进行通信
1、Intent对象详解
Android的应用程序包含三种重要组件:Activity、Service、BroadcastReceiver,应用程序采用一致的方式来启动它们----都是依靠Intent来进行启动的,Intent就封装了程序想要启动程序的意图,不仅如此,Intent还用于与被启动组件进行交换信息。
组件类型
启动方法
Activity
startActivity(Intent intent)
startActivityForResult(Intent intent,intrequestCode)
Service
ComponentName startService(Intent service)
boolean bindService(Intent service,ServiceConnection conn,int flags)
BroadcastReceiver
sendBroadcast(Intent intent)
sendBroadcast(Intent intent,String receiverPermission)
sendOrderedBroadcast(Intent intent,String receiverPermission,BroadcastReceiver resultReceiver,Handler scheler,int initialCode,String initialData,Bundle initialExtras)
sendOrderedBroadcast(Intent intent,String receiverPermission)
sendStickyOrderedBroadcast(Intent intent,BroadcastReceiver resultReceiver,Handler scheler,int initialCode,String initialData,Bundle initialExtras)
Intent对象大致包含Component、Action、Category、Data、Type、Extra和Flag这7种属性,其中Component用于明确指定需要启动的目标组件,而Extra则用于“携带”需要交换的数据。
2、Intent的属性及Intent-filter配置
(1)Component属性
Intent的Component属性需要接受一个ComponentName对象,应用程序可根据给定的组件类去启动特定的组件。
当程序通过Intent的Component属性(明确指定了启动哪个组件)启动特定组件时,被启动组件几乎不需要使用<intent-filter.../>元素进行配置。
(2)Action、Category属性与intent-filter配置
Intent的Action和Category属性都是一个普通的字符串,其中Action代表该Intent所要完成的一个抽象“动作”,Category则用于为Action增加额外的附加类别信息,通常,Action与Category结合使用。
<intent-filter.../>元素里通常可包括如下子元素:
a、0~N个<action.../>子元素
b、0~N个<category.../>子元素
c、0~1个<data.../>子元素
<action.../><category.../>子元素的配置非常简单,它们都可指定android:name属性,该属性的值就是一个普通字符串。
当<activity.../>元素里的<intent-filter.../>子元素里包含多个<action.../>子元素(相当于指定了多个字符串)时,就表明该Activity能响应Action属性值为其中任意一个字符串的Intent。
一个Intent对象最多只能包括一个Action属性,程序调用Intent的setAction(String str)方法来设置Action属性值;但一个Intent对象可包含多个Category属性,调用Intent的addCategory(String str)方法添加。
当程序创建Intent时,该Intent默认启动Category属性值为Intent.CATEGORY_DEFAULT常量(常量值为android.intent.category.DEFAULT)的组件。
(3)指定Action、Category调用系统Activity
实际上,Android内部提供了大量标准Action、Category常量,其中用于启动Activity的标准Action常量及对应的字符串如下:
Action常量
对应字符串
简单说明
ACTION_MAIN
android.intent.action.MAIN
应用程序入口
ACTION_VIEW android.intent.action.VIEW 显示指定数据
ACTION_ATTACH_DATA android.intent.action.ATTACH_DATA 指定某块数据将被附加到其它地方
ACTION_EDIT android.intent.action.EDIT 编辑指定数据
ACTION_PICK android.intent.action.PICK 从列表中选择某项并返回所选的数据
ACTION_CHOOSER android.intent.action.CHOOSER 显示一个Activity选择器
ACTION_GET_CONTENT android.intent.action.GET_CONTENT 让用户选择数据,并返回所选数据
ACTION_DIAL android.intent.action.DIAL 显示拨号面板
ACTION_CALL android.intent.action.CALL 直接向指定用户打电话
ACTION_SEND android.intent.action.SEND 向其他人发送数据
ACTION_SENDTO android.intent.action.SENDTO 向其他人发送消息
ACTION_ANSWER android.intent.action.ANSWER 应答电话
ACTION_INSERT android.intent.action.INSERT 插入数据
ACTION_DELETE android.intent.action.DELETE 删除数据
ACTION_RUN android.intent.action.RUN 运行维护
ACTION_SYNC android.intent.action.SYNC 执行数据同步
ACTION_PICK_ACTIVITY android.intent.action.PICK_ACTIVITY 用于选择Activity
ACTION_SEARCH android.intent.action.SEARCH 执行搜索
ACTION_WEB_SEARCH android.intent.action.WEB_SEARCH 执行Web搜索
ACTION_FACTORY_TEST android.intent.action.FACTORY_TEST 工厂测试的入口点
标准Category常量及对应的字符串如下:
Category常量
对应字符串
简单说明
CATEGORY_DEFAULT android.intent.category.DEFAULT 默认的Category
CATEGORY_BROWSABLE android.intent.category.BROWSABLE 指定该Activity能被浏览器安全调用
CATEGORY_TAB android.intent.category.TAB 指定Activity作为TabActivity的Tab页
CATEGORY_LAUNCHER android.intent.category.LAUNCHER Activity显示顶级程序列表中
CATEGORY_INFO android.intent.category.INFO 用于提供包信息
CATEGORY_HOME android.intent.category.HOME 设置该Activity随系统启动而运行
CATEGORY_PREFERENCE android.intent.category.PREFERENCE 该Activity是参数面板
CATEGORY_TEST android.intent.category.TEST 该Activity是一个测试
CATEGORY_CAR_DOCK android.intent.category.CAR_DOCK 指定手机被插入汽车底座(硬件)时运行该Activity
CATEGORY_DESK_DOCK android.intent.category.DESK_DOCK 指定手机被插入桌面底座(硬件)时运行该Activity
CATEGORY_CAR_MODE android.intent.category.CAR_MODE 设置该Activity可在车载环境下使用
3、Data、Type属性与intent-filter配置
Data属性通常用于向Action属性提供操作的数据,Data属性接收一个Uri对象,一个Uri对象通常通过如下形式的字符串表示:
content://com.android.contracts/contacts/1
tel:123
上面两个字符串的冒号前面大致指定了数据的类型,冒号后面是数据部分。
Type属性则用于明确指定Data属性所指定数据的类型或MIME类型,当Intent不指定Data属性时Type属性才会起作用,否则Android系统将会根据Data属性值来分析数据的类型,因此无需指定Type属性。
一旦为Intent同时指定Action、Data属性,那么Android将可根据指定的数据类型来启动特定的应用程序,并对指定数据执行相应的操作。下面介绍几个Action、Data属性的组合:
ACTION_VIEW content://com.android.contacts/contacts/1:显示标识为1的联系人的信息
ACTION_EDIT content://com.android.contacts/contacts/1:编辑标识为1的联系人的信息
ACTION_DIAL content://com.android.contacts/contacts/1:显示向标识为1的联系人拨号的界面
ACTION_VIEW tel:123:显示向指定号码123拨号的界面
ACTION_DIAL tel:123:显示向指定号码123拨号的界面
ACTION_VIEW content://contacts/people/:显示所有联系人列表的信息,通过这种组合可以方便地查看系统联系人
4、Extra属性
Intent的Extra属性通常用于在多个Action之间进行数据交换,Intent的Extra属性值应该是一个Bundle对象,Bundle对象就像一个Map对象,它可以存入多组key-value对,这样可以就可以通过Intent在不同Activity之间进行数据交换了。
❿ android 使用intent警告 Type safety: Unchecked cast from Serializable to List
intent.putExtra("hello",mList); 这个List<Object>,Object必须是实现Serializable 接口的,你也没贴代码,我只能是这样认为了,这是个类型强转的未经检查的问题。