有时候我们开发的应用希望让其他应用也可以访问,Android平台而言,可以通过Uri(统一资源标识符Uniform Resource Identifier)来实现.

Android中 URI的组成部分scheme, authority and path,其中authority又分为host和port。格式如下: scheme://host:port/path
举个实际的例子:
content://com.dx.test:2020/folder/myfolder/dir
其中scheme 对应 content://
authority 对应 com.dx.test:2020
host 对应 com.dx.test
port 对应 2020
path 对应 /folder/myfolder/dir
这时候我们想到在mainifest.xml里面定义的activity标签下的intent-filter子标签data里面的各个属性,实际上与上面是有对应关系的

 

比如有在A应用程序的mainifest.xml中有这么个Activity

                                                                                     

如上所示,在data里设置了 scheme和host,则该Activity可以接收和处理类似于 "sharetest://data/XXX"的链接。
在A应用程序的TestActivity中编写处理Scheme的逻辑

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;/** * Created by dengxuandeng on 16-3-9. */public class TestActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.test_layout);        Intent a = getIntent();        Log.d("scheme", a.toString());    }}

这里为了方便 直接用log打出来了.
到这里 A 程序就准备OK了.

在B应用程序中,可以直接写一个函数 调起A引用程序中的这个TestActivity

public void gotoScheme(String url) {        Intent intent = new Intent(Intent.ACTION_DEFAULT, Uri.parse(url));        Bundle bundle = new Bundle();        intent.putExtras(bundle);        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);        this.startActivity(intent);    }

调起的时候直接写

gotoScheme("sharetest://data/123141")

那么使用B程序使用Scheme调起 A程序的TestActivity的时候,可以看到A程序的TestActivity打出来的log

03-09 11:35:48.126 1088-1088/com.dear.schemetest D/scheme: Intent { act=android.intent.action.VIEW dat=sharetest://data/123141 flg=0x24000000 cmp=com.dear.schemetest/.TestActivity (has extras) }

更多相关文章

  1. 使用Visual Studio 2015开发Android(安卓)程序
  2. Ubuntu 14.04 设置Android开发环境
  3. android编译系统makefile(Android.mk)写法 (zz)
  4. eclipse中运行安卓程序,提示Failed to allocate memory: 8的解决
  5. 由packagename得到应用程序信息
  6. Android(安卓)关闭多个视图Intent.FLAG_ACTIVITY_CLEAR_TOP用法
  7. 让程序(服务)开机运行
  8. 关于Android应用程序内存泄漏 你需要知道的一切
  9. Activity Task & Android(安卓)lauchMode

随机推荐

  1. Android 获取地理位置的经度和纬度
  2. android通过读取系统属性设置字体缩放的
  3. Android开发之android_apk 在线安装(源代
  4. android 动作处理之手势捕捉
  5. Android解析JSON
  6. android 导出签名APK--混淆文件proguard.
  7. Android Contacts的使用(三)
  8. android traceview and dmtracedump使用
  9. 使用android访问SQLServer数据库
  10. 插件入门,Android(安卓)Studio 生成 Butte