Android中一个app启动另一个app|从浏览器打开app


一、应用A中点击按钮,跳转到应用B

备注:这里是默认启动应用B的启动页面

// 通过包名获取要跳转的app,创建intent对象                        
Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.db_demo");// 这里如果intent为空,就说名没有安装要跳转的应用嘛                        
if (intent != null) {                            
    // 这里跟Activity传递参数一样的嘛,不要担心怎么传递参数,还有接收参数也是跟Activity和Activity传参数一样//                            
    intent.putExtra("name", "Liu xiang");//                            
    intent.putExtra("birthday", "1983-7-13");                            
    startActivity(intent);                        
} else {                            
    // 没有安装要跳转的app应用,提醒一下                            
    ToastUtils.success("哟,赶紧下载安装这个APP吧");                        
}


二、应用A中点击按钮,跳转到应用B中的指定Activity——(scheme方式)

1.应用A中,点击按钮
startActivity(new Intent(Intent.ACTION_VIEW, 
Uri.parse("scheme_db://123123123")));

2.应用B中,AndroidManifest.xml的配置如下
<activity android:name=".ui.WCDBActivity"    
    android:launchMode="singleInstance"> //多窗口,可添加android:launchMode="singleInstance"
    <intent-filter>        
        <category android:name="android.intent.category.DEFAULT" />        
        <action android:name="android.intent.action.VIEW"/>
        <data android:scheme="scheme_db"/>  //通过scheme名称,可启动WCDBActivity
    </intent-filter>
</activity>

3.在应用B中,WCDBActivity中可获取应用A中传递过来的数据
getIntent().getScheme();//获得Scheme名称
String data = getIntent().getDataString();//获得Uri全部路径
Toast.makeText(this, data+"",Toast.LENGTH_LONG).show();


三、应用A点击按钮,跳转到应用B的指定Activity——(指定包名和Activity全路径)

1.应用A中,点击
/**指定包名和带包名的Activity的名字*/
ComponentName componentName = 
    new ComponentName("com.example.db_demo", 
    "com.example.db_demo.ui.SQLiteActivity");
Intent intent = new Intent();
intent.putExtra("id", 1001);
intent.setComponent(componentName);
startActivity(intent);

2.应用B中,AndroidManifest.xml需要修改
<activity 
    android:name=".ui.SQLiteActivity"    
    android:exported="true" //必须配置,否则不能被其他应用开启
    //如果想要在相邻窗口打开,则修改启动模式singleTask或者singleInstance
    android:launchMode="singleTask"/>


四、通过浏览器打开Android App 应用

原理同方法二,使用scheme

1、首先做成HTML的页面,页面内容格式例如以下:
<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a> 

各个项目含义例如以下所看到的:
scheme:判别启动的App。 ※具体后述
host:适当记述
path:传值时必须的key ※没有也能够
query:获取值的Key和Value ※没有也能够

作为测试例如以下:

<a href="myapp://jp.app/openwith?name=zhangsan&age=26">启动应用程序</a>
2、app应用中,待打开的Activity在AndroidManifest.xml的配置如下:
<intent-filter>  
    <action android:name="android.intent.action.VIEW"/>  
    <category android:name="android.intent.category.DEFAULT" />  
    <category android:name="android.intent.category.BROWSABLE" />  
    <data 
    	android:scheme="myapp" 
    	android:host="jp.app" 
    	android:pathPrefix="/openwith"/>  
</intent-filter>
3、接下来在Activity中须要取值的地方加入下面代码:
Intent i_getvalue = getIntent();  
String action = i_getvalue.getAction();  
  
if(Intent.ACTION_VIEW.equals(action)){  
    Uri uri = i_getvalue.getData();  
    if(uri != null){  
        String name = uri.getQueryParameter("name");  
        String age= uri.getQueryParameter("age");  
    }  
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值