前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android 通过类名跳转activity,Activity跳转方式总结

android 通过类名跳转activity,Activity跳转方式总结

作者头像
全栈程序员站长
发布2022-08-31 15:24:15
8930
发布2022-08-31 15:24:15
举报

大家好,又见面了,我是你们的朋友全栈君。

一、显式调用方法

方法一:

Intent intent=new Intent(本类,将要跳转的类); //Intent intent=new Intent(MainActivity.this,JumpToActivity.class);

startActivity(intent);

方法二:

Intent intent2=new Intent();

intent2.setClass(本类,将要跳转的类); // intent2.setClass(MainActivity.this,JumpToActivity.class);

startActivity(intent2);

方法三:(此方式可用于打开其它的应用)

Intent intent2=new Intent();

intent2.setComponent(new ComponentName(MainActivity.this, JumpToActivity.class));

startActivity(intent2);

component,目标组件的包或类名称(完整类名):

在使用component进行匹配时,一般采用以下几种形式:

intent.setComponent(new ComponentName(getApplicationContext(), JumpToActivity.class));

intent.setComponent(new ComponentName(getApplicationContext(), “com.liujc.test.JumpToActivity”));

intent.setComponent(new ComponentName(“com.liujc.test”, “com.liujc.test.JumpToActivity”));

二:隐式调用方法

通过action跳转:

Intent intent = new Intent();

intent.setAction(“con.liujc.test.jump”);

startActivity(intent);

需要将要跳转到的Activity在AndroidManifest.xml中设置action:

通过Scheme跳转协议跳转:

android中的scheme是一种页面内跳转协议,是一种非常好的实现机制,通过定义自己的scheme协议,可以非常方便跳转app中的各个页面;通过scheme协议,服务器可以定制化告诉App跳转那个页面,可以通过通知栏消息定制化跳转页面,可以通过H5页面跳转页面等。

URL Scheme协议格式:

liujc://goods:8080/goodsDetail?goodsId=20170112

上面的路径 Scheme、Host、port、path、query全部包含:

liujc代表该Scheme 协议名称

goods代表Scheme作用于哪个地址域

goodsDetail代表Scheme指定的页面

goodsId代表传递的参数

8080代表该路径的端口号

URL Scheme如何使用:

在AndroidManifest.xml中对标签增加设置Scheme:

android:name=”.GoodsDetailActivity”

android:theme=”@style/AppTheme”>

获取Scheme跳转的参数:

Uri uri = getIntent().getData();

if (uri != null) {

// 完整的url信息

String url = uri.toString();

Log.e(TAG, “url: ” + uri);

// scheme部分

String scheme = uri.getScheme();

Log.e(TAG, “scheme: ” + scheme);

// host部分

String host = uri.getHost();

Log.e(TAG, “host: ” + host);

//port部分

int port = uri.getPort();

Log.e(TAG, “host: ” + port);

// 访问路劲

String path = uri.getPath();

Log.e(TAG, “path: ” + path);

List pathSegments = uri.getPathSegments();

// Query部分

String query = uri.getQuery();

Log.e(TAG, “query: ” + query);

//获取指定参数值

String goodsId = uri.getQueryParameter(“goodsId”);

Log.e(TAG, “goodsId: ” + goodsId);

}

调用方式:

网页上:(使用系统自带浏览器或者谷歌浏览器)

打开商品详情

原生调用:

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(“liujc://goods:8080/goodsDetail?goodsId=20170112”));

startActivity(intent);

如何判断一个Scheme是否有效,有效后再启动:

PackageManager packageManager = getPackageManager();

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(“liujc://goods:8080/goodsDetail?goodsId=20170112”));

List activities = packageManager.queryIntentActivities(intent, 0);

boolean isValid = !activities.isEmpty();

if (isValid) {

startActivity(intent);

}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/143245.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档