开发者

Android: Filter intent for specific Uri pattern

开发者 https://www.devze.com 2023-01-23 13:08 出处:网络
I need my Android application to run a certain Activity in my app, responding to the following Uri data from the Intent that was sent out:

I need my Android application to run a certain Activity in my app, responding to the following Uri data from the Intent that was sent out:

http://www.example.com/redirect.aspx?customurl=example%3a%2f%2f%3fop%3dexampledetail%26stuff%3d12345%26morestuff%3dI%2520Love%25Android

If I use the following in my manifest (for the Activity that I want to respond to the Intent), I can capture this, but the chooser pops up (which I don't want):

<intent-filter>
 <action android:name="android.intent.action.VIEW" />
 <data android:scheme="http" android:host="www.example.com" />
 <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Since I don't want the chooser to pop up, I tried to use the android:pathPattern, or android:pathPrefix in the tag to further filter 开发者_StackOverflow中文版to make sure that only my app responds but it isn't working for me.

Can anyone help me do this?


If I use the following in my manifest (for the Activity that I want to respond to the Intent), I can capture this, but the chooser pops up (which I don't want):

Most Android devices will have 1+ browsers on them. The user is perfectly welcome to visit that URL in one of those browsers instead of your app, which is why the chooser appears. Moreover, it would be a massive security hole if developers were able to hijack any HTTP URL they wanted.

Now, if you own the Web site for which this URL links to, you can use the trick that Barcode Scanner uses: use browser detection to put smarts on that page that, if viewed in an Android browser, explains to the user why they should allow your app to handle that URL in the future.


It looks like your dealing with data which comes from your own application, so don't use your "private" URL as the target of the intent but create a component specific Intent and give it the URL of your data as an EXTRA parameter.

Something like:

Uri privateUri = Uri.parse("http://yourserver.com/path/to/data");
Intent i = new Intent(context, yourActivity.class);
i.putExtra("DATA_URI", privateUri);
startActivity(i);

In yourActivity.class, you would just have to get the Uri with the following code:

Uri privateUri = (Uri) getIntent().getParcelableExtra("DATA_URI");
0

精彩评论

暂无评论...
验证码 换一张
取 消