android 第三方应用 启动app

本地应用manifest的设置

Manifest中 Data的语法:
<data android:host="string"
      android:mimeType="string"
      android:path="string"
      android:pathPattern="string"
      android:pathPrefix="string"
      android:port="string"
      android:scheme="string" />

Uri的格式:scheme://host:port/path or pathPrefix or pathPattern

如果scheme没有指定,那其它的属性均无效;

如果host没有指定,那么port,path,pathPrefix,pathPattern均无效;

如果在manifest里这样写:<data android:scheme="something" android:host="project.example.com" />

那么Uri uri = Uri.parse("something://project.example.com"); 才可以匹配

再如:
<data android:scheme="something" android:host="project.example.com" android:port="80"/>
等同于这样写:
<data android:scheme="something"/>
<data android:host="project.example.com"/>
<data android:port="80"/>
那么Uri uri = Uri.parse("something://project.example.com:80"); 才可以匹配

可以有多个data,只需匹配其中一个即可
<activity android:name=".MyActivityTwo" android:label="@string/activityTwo">
<intent-filter>
             <action android:name="android.intent.action.leo"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
                <data android:scheme="x-id"/>
                <data android:scheme="something"/>
</intent-filter>
</activity>

启动方式

android中第三方应用启动本地应用的方式有三种:1、通过包名启动 2、通过scheme启动 3、通过webview启动

包名启动

方式一:

Intent  mIntent =new Intent();
mIntent.setClassName("包名", "要启动的activity(全称)");
startActivity(mIntent);

注意:要启动的Activity需要设置 android:exported="true"的属性,支持外部应用打开。而通过scheme启动则不需要。

 

注意:要启动的Activity需要设置 android:exported="true"的属性,支持外部应用打开。而通过scheme启动则不需要。

方式二:

Intent LaunchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
context.startActivity(LaunchIntent);

这个方式我感觉类似于双击home键之后选中应用,从activity栈里面把之前的页面展示到前台栈中。

 

通过scheme启动

跳转到我们系统中的一些标准功能都是通过scheme跳转实现的,比如跳转到打电话,跳转到短信等。

Intent dialIntent =  new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + phoneNumber));//直接拨打电话
startActivity(dialIntent);

跳转到我们本地应用:

Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse("warehouse://?where_shotcut="));
context.startActivity(it);

格式含义:

scheme://host:8080/path?query1=1&query2=true

● scheme代表Scheme协议名称

● host代表Scheme作用的地址域

● 8080代表改路径的端口号

● path代表的是指定页面(路径)

● query1和query2代表传递的两个参数

通过webview启动

String url ="ntclound://nthost?data1=1&data2=2";
mWebView.loadUrl(url);

//覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开
mWebView.setWebViewClient(new WebViewClient() {
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // TODO Auto-generated method stub
    //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器

    if (!url.startsWith("http")) {
      try {
        // 以下固定写法,表示跳转到第三方应用
        final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        getApplication().startActivity(intent);
      } catch (Exception e) {
        // 防止没有安装的情况
        e.printStackTrace();
      }
      return true;
    }else{
      view.loadUrl(url);
    }
    return true;
  }
  ....
}

本地应用获取scheme启动带来的参数

// 启动代码
webview.loadUrl("scheme://host?key1=参数1&key2=参数2");

// 本地应用解析
Intent intent = getIntent();
String scheme = intent.getScheme();
Uri uri = intent.getData();
System.out.println("scheme:"+scheme);
if (uri != null) {
String host = uri.getHost();
String dataString = intent.getDataString();
//获得参数值
String key1 = uri.getQueryParameter("key1");
String key2 = uri.getQueryParameter("key2");
}  

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 6.0及以上版本中,需要动态申请权限才能访问存储空间。以下是一个示例代码,用于启动第三方应用并请求存储权限: ```java private static final int REQUEST_CODE_STORAGE_PERMISSION = 1; // 启动第三方应用 private void launchThirdPartyApp() { Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.thirdpartyapp"); if (launchIntent != null) { if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // 如果已经授权,直接启动第三方应用 startActivity(launchIntent); } else { // 如果没有授权,请求存储权限 requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_STORAGE_PERMISSION); } } else { // 第三方应用未安装 Toast.makeText(this, "Third party app not installed.", Toast.LENGTH_SHORT).show(); } } // 处理权限请求结果 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // 用户已授权,启动第三方应用 Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.thirdpartyapp"); startActivity(launchIntent); } else { // 用户拒绝授权,无法启动第三方应用 Toast.makeText(this, "Permission denied, cannot launch third party app.", Toast.LENGTH_SHORT).show(); } } } ``` 在上面的代码中,`launchThirdPartyApp()` 方法会尝试启动指定包名的第三方应用。如果当前应用已经被授予存储权限,直接启动第三方应用;否则,请求存储权限,并在 `onRequestPermissionsResult()` 方法中处理用户的授权结果。如果用户授权,再次尝试启动第三方应用;如果用户拒绝授权,提示用户无法启动第三方应用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值