android隐式启动Activity的例子
  9ElbUmxx8qzo 2023年11月02日 62 0


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

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  5HAFzPFxGStu   2023年11月12日   25   0   0 githubHTMLgit
  WuxuVoSAjref   2023年11月02日   38   0   0 HTMLWeb
9ElbUmxx8qzo