首页 > 其他分享 >android隐式启动Activity的例子

android隐式启动Activity的例子

时间:2023-07-31 17:38:15浏览次数:42  
标签:DEFAULT Uri uri parse Intent Activity intent android 隐式


android隐式启动Activity的例子【原创】





android2.2测试通过

android隐匿启动Activity的例子,同样适用于Service,BroadcastReceiver

<activity android:name=".MyActivityTwo" android:label="ThisMyActivityTwo">

<!--

这样进行匹配:

Intent intent = new Intent(Intent.ACTION_VIEW);//等同于 new Intent( "android.intent.action.VIEW")
Uri uri = Uri.parse("something://project.example.com2:80");//可以
Uri uri = Uri.parse("something:");//这个不行
Uri uri = Uri.parse("something://project.example.com2");//这个不行
intent.setData(uri);
startActivity(intent);
-->
<!--

下面的写法等同于:

<data android:scheme="something"/>
<data android:host="project.example.com"/>
<data android:port="80"/> 
-->
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<data android:scheme="something" android:host="project.example.com2"
android:port="80" />
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>

<!--
按下面的方式进行匹配:
Intent intent = new Intent(Intent.ACTION_VIEW);//等同于 new Intent( "android.intent.action.VIEW")
intent.addCategory(Intent.CATEGORY_DEFAULT);// 可去掉,因为Category默认值即系Intent.CATEGORY_DEFAULT
//下面的setType中,选其中任何一个都可
//intent.setType("leo.android.cursor.dir/vnd.google.leo");
intent.setType("leo.android.cursor.dir/vnd.google.liao");
startActivity(intent); 
-->
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<data android:mimeType="leo.android.cursor.dir/vnd.google.leo" />
<data android:mimeType="leo.android.cursor.dir/vnd.google.liao" />
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>


<!--

按下面的方式即可匹配,因为这个action hello.hi.liao是android系统里唯一的,所以很容易就能匹配上

Intent intent = new Intent("hello.hi.liao");
intent.addCategory(Intent.CATEGORY_DEFAULT);// 可去掉,因为Category默认值即系Intent.CATEGORY_DEFAULT
startActivity(intent); 
-->
<intent-filter>
<action android:name="hello.hi.liao"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>

<!--
按下面这样写即可匹配:
//选下面两个uri中的其中任何一个都可
Uri uri = Uri.parse("content8://sdcard/leo/helloleo.txt");
Uri uri = Uri.parse("file8://sdcard/leo/helloleo.txt");
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory(Intent.CATEGORY_DEFAULT);// 可去掉,因为Category默认值即系Intent.CATEGORY_DEFAULT
//选下面四个中的其中任何一个都可
intent.setDataAndType(uri, "text/htmlleo8");
intent.setDataAndType(uri, "text/plainleo8");
intent.setDataAndType(uri, "application/xhtml+xml+leo8");
intent.setDataAndType(uri, "application/vnd.wap.xhtml+xml+leo8");
startActivity(intent); 
-->
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="file8" />
<data android:scheme="content8" />
<data android:mimeType="text/htmlleo8" />
<data android:mimeType="text/plainleo8" />
<data android:mimeType="application/xhtml+xml+leo8" />
<data android:mimeType="application/vnd.wap.xhtml+xml+leo8" />
</intent-filter>




<!-- 

这样写可以匹配:

Uri uri = Uri.parse("contentleo10://sdcard/leo/helloleo.txt");
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory(Intent.CATEGORY_DEFAULT);// 可去掉,因为Category默认值即系Intent.CATEGORY_DEFAULT
intent.setDataAndType(uri, "text/helloleo10");
startActivity(intent);
-->
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="contentleo10" />
<data android:mimeType="text/helloleo10" />
</intent-filter>




<!--

这样写可以匹配:

Uri uri = Uri.parse("contentleo11://project.example.com11/sdcard/leo/helloleo.txt");
//这样写不行,因为已指出需要host匹配,所以必须得写上
Uri uri = Uri.parse("contentleo11://sdcard/leo/helloleo.txt");
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory(Intent.CATEGORY_DEFAULT);// 可去掉,因为Category默认值即系Intent.CATEGORY_DEFAULT
intent.setDataAndType(uri, "text/helloleo11");
startActivity(intent);
-->
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="contentleo11" />
<data android:host="project.example.com11" />
<data android:mimeType="text/helloleo11" />
</intent-filter>






<!--

这个可以匹配到MyActivityTwo:

Uri uri = Uri.parse("leohellocontent://project.example.com:100/sdcard/leo/helloleo.txt");

或者这样去掉port项也可以:

Uri uri = Uri.parse("leohellocontent://project.example.com/sdcard/leo/helloleo.txt");

下面这样不行,因为指明要host,而没有写出来

Uri uri = Uri.parse("leohellocontent://sdcard/leo/helloleo.txt");
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory(Intent.CATEGORY_DEFAULT);// 可去掉,因为Category默认值即系Intent.CATEGORY_DEFAULT
intent.setDataAndType(uri, "text/leohello");
startActivity(intent); 
-->
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="leohellocontent" />
<data android:host="project.example.com" />
<data android:port="100" />
<data android:mimeType="text/leohello" />
</intent-filter>





<!--

下面这个无论你怎样写uri都匹配不上,不知道什么原因(难道是不可以同时有host,port,path还是uri的格式写法有误?)

Uri uri = Uri.parse("leohellocontent12://project.example.com/sdcard/leo/helloleo.txt");
Uri uri = Uri.parse("leohellocontent12://project.example.com/leopath/sdcard/leo/helloleo.txt");
Uri uri = Uri.parse("leohellocontent12://project.example.com:200/leopath/sdcard/leo/helloleo.txt");
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory(Intent.CATEGORY_DEFAULT);// 可去掉,因为Category默认值即系Intent.CATEGORY_DEFAULT
intent.setDataAndType(uri, "text/leohello12");
startActivity(intent); 
-->
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="contentleo12" />
<data android:host="project.example.com12" />
<data android:port="200" />
<data android:path="leopath" />
<data android:mimeType="text/helloleo12" />
</intent-filter>


</activity>


在被启动的Activity(本例为MyActivityTwo)里接收数据:

Intent intent = getIntent();
String intentCategories = intent.getCategories()
String intentType = intent.getType();
Uri uri = intent.getData();
String uriScheme = uri.getScheme();
String uriPath = uri.getPath();
String uriHost = uri.getHost();
String uriEncodedPath = uri.getEncodedPath();

请参考:packages\apps\HTMLViewer\src\com\android\htmlviewer\HTMLViewerActivity.java

.html

标签:DEFAULT,Uri,uri,parse,Intent,Activity,intent,android,隐式
From: https://blog.51cto.com/u_3124497/6910421

相关文章

  • Android学 App自动更新之通知栏下…
    Android学习系列(2)--App自动更新之通知栏下载见证过博客园的多次升级,你也希望你的软件通过更新发布新特性通知用户吧,是的。这篇文章是android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用。 1.设计思路,使用VersionCode定义为版本升级参数。android为我......
  • Android中dispatchTouchEvent,&nbs…
    onInterceptTouchEvent用于改变事件的传递方向。决定传递方向的是返回值,返回为false时事件会传递给子控件,返回值为true时事件会传递给当前控件的onTouchEvent(),这就是所谓的Intercept(拦截)。[tisaps:正确的使用方法是,在此方法内仅判断事件是否需要拦截,然后返回。即便需要拦截......
  • Android应用如何适配不同分辨率的…
           Android应用如何适配不同分辨率的手机主要分三块考虑1)界面配置根据不同的分辨率,创建手机界面文件例子:在res下创建layout-800x480    ......
  • Android开发FAQ-ContentObserver应…
    一、需求1.产品不同界面显示数据个数.2.数据个数动态改变.3.涉及数据与本地数据库有关可采用ContentObserver技术实现二、代码实现如下:mCursorObserver=newContentObserver(mHandler){          @Override          publicvoidonChange(booleanse......
  • Android 设计模式:(二)观察者模…
    Android设计模式:(二)观察者模式——让你的对象知悉现况设计模式2012-05-2813:28 1074人阅读 评论(1) 收藏 举报*观察者模式:定义了对象之间的一对多依赖关系,当一个对象(主题对象)的状态改变时,它的所有依赖者(观察者对象)都会收到通知并自动更新。*观察......
  • Android NFC Mifare Tag 读写示例
    前面例子介绍了检测,读写NFCTAG开发的一般步骤,本例针对常用的MifareTag具体说明。MifareTag可以有1K,2K,4K,其内存分区大同小异,下图给出了1K字节容量的Tag的内存分布:数据分为16个区(Sector),每个区有4个块(Block),每个块可以存放16字节的数据,其大小为16X4X16=1024......
  • 关于Android流畅度不如iOS的几点看…
    关于Android流畅度不如iOS的几点看法网上一名据称是前谷歌实习生的人透露了一些关于Android系统硬件加速的内幕。据称,在Android3.0和4.0之前,并没有完整的硬件加速。他们一直在通过硬件加速绘制某些UI元素,并称效果并不像他们想象中那样乐观。他认为,Android效率低下的设计框......
  • Android应用开发的插件化 模块化
    在android的项目开发中,都会遇到后期功能拓展增强与主程序代码变更的现实矛盾,也就是程序的灵活度。  由于linux平台的安全机制,再加上dalvik的特殊机制,各种权限壁垒,使得开发一个灵活多变的程序,变得比较困难,不像pc平台下那么容易。  瞅瞅elipse的插件,瞅瞅360的插件,在an......
  • Android解耦(四)基于依赖注入的解耦
    安卓开发中基于依赖注入(DI)的模块解耦1.什么是依赖注入(DI)依赖注入(DependencyInjection,简称DI)是一种设计模式,用于实现控制反转(InversionofControl,简称IoC)。控制反转是指将对象之间的依赖关系由程序代码中定义转移到外部容器中管理,从而降低对象之间的耦合度,提高代码的可维护性和......
  • JDK 版本异常导致 flutter doctor --android-licenses 出错 (class file version 61.0
    flutterdoctor--android-licensesError:AJNIerrorhasoccurred,pleasecheckyourinstallationandtryagainExceptioninthread"main"java.lang.UnsupportedClassVersionError:com/android/sdklib/tool/sdkmanager/SdkManagerClihasbeencompil......