android隐式启动Activity的例子

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

android2.2测试通过

更多说明:http://hi.baidu.com/wishwingliao/blog/item/013b9713733d3c956538dbe0.html?timeStamp=1302236607151

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

转至:http://hi.baidu.com/wishwingliao/blog/item/0a38ccfce06f39e8fc037f85.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值