前言:
要打开文件,就要利用 Intent 这个类,再加上文件的Uri 和 MIME Type去寻找对应的程序打开。
那么Intent这个类是什么?怎么使用?
Intent是什么我就不做介绍了。以下是我对Intent使用的一些代码
显式Intent:带有包名和componet的都属于显式,都指名道姓了
隐式Intent:没有指出组件或者包名等明确名称的就是隐式
那么这里设置了 setAction和addCategory 到底是打开哪个Activity?
我们看看Manifest.xml
这篇文章纯粹个人理解,作为学习笔记。如有不足或者错误的地方,敬请谅解。如有指出,本人十分感谢并乐意虚心学习。
上次做了一个资源管理器,发现自己对寻找对应程序打开文件这方面有好多缺陷。
要打开文件,就要利用 Intent 这个类,再加上文件的Uri 和 MIME Type去寻找对应的程序打开。
那么Intent这个类是什么?怎么使用?
Intent是什么我就不做介绍了。以下是我对Intent使用的一些代码
显式Intent:带有包名和componet的都属于显式,都指名道姓了

//显式Intent
//第一种利用构造函数创建
Intent intent
=
new Intent(MainActivity.
this, SecondActivity.
class);
Intent intent
=
new Intent(
"com.example.administrator.ACTION_START",
null,
MainActivity.
this, SecondActivity.
class);
Intent intent
=
new Intent();
//第二种 利用Intent 自带的方法
//setClass
intent.setClass(MainActivity.
this, SecondActivity.
class);
//setClassName
intent.setClassName(MainActivity.
this,
"com.example.administrator.intentexplorer.SecondActivity");
intent.setClassName(
"com.example.administrator.intentexplorer",
"com.example.administrator.intentexplorer.SecondActivity");
//setComponent
ComponentName cn
=
new ComponentName(MainActivity.
this, SecondActivity.
class);
ComponentName cn
=
new ComponentName(MainActivity.
this,
"com.example.administrator.intentexplorer.SecondActivity");
ComponentName cn
=
new ComponentName(
"com.example.administrator.intentexplorer",
"com.example.administrator.intentexplorer.SecondActivity");
intent.setComponent(cn);
//隐式Intent
//打开另一个活动
Intent intent
=
new Intent();
intent.setAction(
"com.example.administrator.ACTION_START");
intent.addCategory(
"com.example.administrator.MY_CATEGORY");
我们看看Manifest.xml
上
下
省
略
.........
<activity
android:name
=
".SecondActivity"
android:label
=
"@string/title_activity_second"
>
<intent
-filter
>
<action android:name
=
"com.example.administrator.ACTION_START" /
>
<category android:name
=
"android.intent.category.DEFAULT" /
>
</intent
-filter
>
</activity
>
<activity
android:name
=
".ThridActivity"
android:configChanges
=
"orientation|keyboardHidden|screenSize"
android:label
=
"@string/title_activity_thrid"
android:theme
=
"@style/FullscreenTheme"
>
<intent
-filter
>
<action android:name
=
"com.example.administrator.ACTION_START" /
>
<