获取个人应用代码并显示

2023-12-24

我正在尝试获取应用程序代码并显示它,例如,如果按钮 X 启动一个新活动,则 textView 显示整个方法

我只到达了如何以 HTML 格式显示代码这个问题 https://stackoverflow.com/questions/1529068/is-it-possible-to-have-multiple-styles-inside-a-textview

但是有没有办法获取我的应用程序的代码,我认为有两种方法

  1. 内部的一个通过应用程序本身获取它
  2. 外部一个通过读取java文件然后过滤它并获取方法的文本

对此有什么想法吗?

提前致谢


正如其他人评论中提到的那样,上述目前不可能。我可以建议的是,将您的应用程序与资产文件夹中的源代码一起发送,并使用辅助函数在运行时从源代码中提取某些方法(您提出的第二种方法)。我已经编写了示例代码,但它是纯java的,需要移植到android(几行)。

注意:根据您的用例,您可能需要在提取后重新格式化代码。

希望能帮助到你 :)

辅助方法的代码:

static String getTheCode(String classname ,String methodSignature ) throws FileNotFoundException {

     //**********************A few lines of code below need changing when porting ***********//

    // open file, your will be in the assets folder not in the home dir of user, don't forget the .java extension when porting

    File file = new File(System.getProperty("user.home") +"/"+ classname +".java");

    // get the source, you can use FileInputReader or some reader supported by android

    Scanner scanner = new Scanner(file);

    String source = "";
    while(scanner.hasNext()) {
       source += " "+ scanner.next();
    }


    //**********************The above code needs changing when porting **********//

    // extract code using the method signature

    methodSignature = methodSignature.trim();
    source = source.trim();

    //appending { to differentiate from argument as it can be matched also if in the same file
    methodSignature = methodSignature+"{";

    //making sure we find what we are looking for
    methodSignature = methodSignature.replaceAll("\\s*[(]\\s*", "(");
    methodSignature = methodSignature.replaceAll("\\s*[)]\\s*", ")");
    methodSignature = methodSignature.replaceAll("\\s*[,]\\s*", ",");
    methodSignature = methodSignature.replaceAll("\\s+", " ");


    source =source.replaceAll("\\s*[(]\\s*", "(");
    source = source.replaceAll("\\s*[)]\\s*", ")");
    source = source.replaceAll("\\s*[,]\\s*", ",");
    source = source.replaceAll("\\s+", " ");


    if(!source.contains(methodSignature)) return null;

    // trimming all text b4 method signature
    source = source.substring(source.indexOf(methodSignature));

    //getting last index, a methods ends when there are matching pairs of these {}
    int lastIndex = 0;

    int rightBraceCount = 0;
    int leftBraceCount = 0;

    char [] remainingSource = source.toCharArray();
    for (int i = 0; i < remainingSource.length ; i++
         ) {

        if(remainingSource[i] == '}'){

            rightBraceCount++;

            if(rightBraceCount == leftBraceCount){

                lastIndex = (i + 1);
                break;
            }

        }else if(remainingSource[i] == '{'){

            leftBraceCount++;
        }




    }


    return  source.substring(0 ,lastIndex);

}

用法示例(getTheCode 方法是静态的,位于名为 GetTheCode 的类中):

public static void main(String... s) throws FileNotFoundException {



    System.out.println(GetTheCode.getTheCode("Main", "private static void shoutOut()"));
    System.out.println(GetTheCode.getTheCode("Main", "private static void shoutOut(String word)"));


}

Output:

private static void shoutOut(){ // nothing to here }
private static void shoutOut(String word){ // nothing to here }

注意:开始新活动时创建一个方法,例如

 private void myStartActivty(){

 Intent intent = new Intent(MyActivity.this, AnotherActivity.class);

    startActivity(intent);

}

然后在你的 onClick 中:

@Override
public void onClick(View v) {
    myStartActivity();

    myTextView.setText(GetTheCode.getTheCode("MyActivity","private void myStartActivity()"));
}

更新:移植了android的代码:

    import android.content.Context;

    import java.io.IOException;
    import java.util.Scanner;

    public class GetTheCode {


static String getTheCode(Context context, String classname , String methodSignature ) {

   Scanner scanner = null;
    String source = "";
    try {
        scanner = new Scanner(context.getAssets().open(classname+".java"));



    while(scanner.hasNext()) {
       source += " "+ scanner.next();
    }

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
        scanner.close();

    // extract code using the method signature

    methodSignature = methodSignature.trim();
    source = source.trim();

    //appending { to differentiate from argument as it can be matched also if in the same file
    methodSignature = methodSignature+"{";

    //making sure we find what we are looking for
    methodSignature = methodSignature.replaceAll("\\s*[(]\\s*", "(");
    methodSignature = methodSignature.replaceAll("\\s*[)]\\s*", ")");
    methodSignature = methodSignature.replaceAll("\\s*[,]\\s*", ",");
    methodSignature = methodSignature.replaceAll("\\s+", " ");


    source =source.replaceAll("\\s*[(]\\s*", "(");
    source = source.replaceAll("\\s*[)]\\s*", ")");
    source = source.replaceAll("\\s*[,]\\s*", ",");
    source = source.replaceAll("\\s+", " ");


    if(!source.contains(methodSignature)) return null;

    // trimming all text b4 method signature
    source = source.substring(source.indexOf(methodSignature));

    //getting last index, a methods ends when there are matching pairs of these {}
    int lastIndex = 0;

    int rightBraceCount = 0;
    int leftBraceCount = 0;

    char [] remainingSource = source.toCharArray();
    for (int i = 0; i < remainingSource.length ; i++
         ) {

        if(remainingSource[i] == '}'){

            rightBraceCount++;

            if(rightBraceCount == leftBraceCount){

                lastIndex = (i + 1);
                break;
            }

        }else if(remainingSource[i] == '{'){

            leftBraceCount++;
        }




    }


    return  source.substring(0,lastIndex);

   }

}

Usage:

   // the method now takes in context as the first parameter, the line below was in an Activity
  Log.d("tag",GetTheCode.getTheCode(this,"MapsActivity","protected void onCreate(Bundle savedInstanceState)"));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

获取个人应用代码并显示 的相关文章

  • 当活动被破坏时如何保存状态

    public class Talk extends Activity private ProgressDialog progDialog int typeBar TextView text1 EditText edit Button res
  • 由于现有相机用户,相机“0”的手电筒不可用

    我想创建一个应用程序 它有一个用于录制视频的按钮和另一个单独的切换按钮 用于在录制视频期间打开闪光灯 我已经使用camera2 API为最近的androids构建了相机应用程序 可以通过图像按钮访问 我将火炬模式设置为切换按钮 但这根本不起
  • 单击另一个项目/小部件时展开/打开微调器?

    当用户单击另一个按钮时 我试图展开微调器 例如 我有一个带有值的微调器和一个 确定 按钮 当用户单击 确定 按钮而不从微调器中选择任何值时 微调器会自行扩展 是否可以在无需用户与微调器交互的情况下获得扩展微调器的事件 只需致电Spinner
  • 禁用 com.google.android.maps.MapView 中的平移/缩放

    如何禁用 MapView 的平移 缩放功能 不是缩放控件 我想要一个完全静态的地图 我还注意到触摸地图似乎不会触发 MapView onClickListener 有人可以详细说明为什么吗 对于 Android 版 Google Maps
  • 垂直 ViewPager 中的动画

    我需要垂直制作这个动画ViewPager https www youtube com watch v wuE 4jjnp3g https www youtube com watch v wuE 4jjnp3g 这是我到目前为止所尝试的 vi
  • Android 软键盘 - 禁用某些键

    我正在寻找一种使用内置软键盘并禁用某些键的方法 例如 如果用户不应该使用字母 f 因为在列表中该字母不存在 则该键应显示为灰色 想象一下 用户可以在文本框中键入文本以从列表中进行选择 该列表包含 ABC BCCD 床 如果用户输入 A 然后
  • 如何使用onDraw(Canvas)获取WebView的位图快照(Android)

    我曾经使用 capturePicture 方法来制作 WebView 的快照 此方法在 API 级别 19 中已弃用 该文档说 使用 onDraw Canvas 获取 WebView 的位图快照 但我真的不知道它是什么意思 你能教我如何解决
  • 获取Android库中的上下文

    我正在编写一个 Android 应用程序 它的一些功能封装在内部库中 但是 要使此功能发挥作用 库需要一个应用程序上下文的实例 为图书馆提供这种上下文的最佳方式是什么 我看到了一些选择 但没有一个有吸引力 Have my library c
  • 改造将多个图像上传到单个密钥

    我正在使用 Retrofit 将图像上传到我的服务器 这里我需要为一个密钥上传多个图像 我已经尝试使用 Postman 网络客户端 它运行良好 这是一个屏幕截图 以下是请求的键值对 调查图像 文件1 文件2 文件3 属性图像 文件DRA j
  • 如何告诉 OkHttpClient 忽略缓存并强制从服务器刷新?

    在我的 Android 应用程序中 我将 Retrofit 与 OkHttpClient 结合使用 并启用缓存来访问某些 API 我们的一些 API 有时会返回空数据 我们在应用程序中提供了一个 刷新 按钮 供客户端从特定 API 重新加载
  • Google Wallet for Digital Goods API 与 Google Play 应用内结算

    想知道 Google 电子钱包结算 API 和 Google Play 应用内结算之间有什么区别 与 Google 电子钱包结算 API 相比 使用 GooglePlay 应用内购买结算服务有何优势 我看到 Wallet API 也支持 A
  • Android 应用程序不需要任何特殊访问权限

    当我开始安装时myapp apk 我得到下面的屏幕 我的应用程序需要位置 外部存储权限 上述权限应该根据需要向用户请求 即在需要这些权限的代码之前 现在 当安装应用程序时 我会看到一个屏幕 上面显示应用程序不需要任何特殊访问权限 如下图所示
  • 以编程方式创建 FloatingActionButton(无需 xml)

    我很欣赏 Android 的 FloatingActionButton fab 功能 并希望在我的项目中的许多不同地方使用它们 现在 我有这样的东西 我有几个 xml 规范 除了 id 图标和 onclick 之外 所有这些规范都是相同的
  • 线性布局高度和重量

    我有以下内容
  • Android Studio - 无法解析符号“firebase”

    我目前正在将应用程序升级到新的 Firebase 版本 我按照指南进行操作 包括classpath com google gms google services 3 0 0 在我的项目 build gradle 的依赖项中以及compile
  • Android:是否可以在可绘制选择器中使用字符串/枚举?

    问题 Q1 有人设法让自定义字符串 枚举属性在 xml 选择器中工作吗 我通过以下 1 获得了一个布尔属性 但不是字符串属性 编辑 感谢您的回答 目前 android 仅支持布尔选择器 原因请参阅已接受的答案 我计划实现一个复杂的自定义按钮
  • Dart/Flutter 如何编译到 Android?

    我找不到任何具体的资源 Dart 是否被编译到 JVM 或者 Google 的团队是否编译了 Dart VM 以在 JVM 上运行 然后在 JVM 内的 Dart VM 中运行 Dart 前者更有意义 并且符合 无桥 的口号 但后者似乎更符
  • 如何在android中将文本放在单选按钮的左侧

    我想将单选按钮的文本放在左侧而不是右侧 我找到了这个解决方案
  • 从 Dropbox 下载文件并将其保存到 SDCARD

    现在我真的很沮丧 我想从 Dropbox 下载一个文件并将该文件保存到 SD 卡中 我得到的代码为 private boolean downloadDropboxFile String dbPath File localFile throw
  • 连接到具有相同 SSID 的最强接入点(信号最强的接入点)

    我正在编写一个程序来始终连接到最强的接入点 我的意思是信号最强的接入点 首先 我扫描所有可用的 WiFi 网络 然后限制它们仅查看具有相同 SSID 的网络 这样我就可以看到一个网络的所有AP 当我连接到该网络时 它没有连接到最强的信号 但

随机推荐