【问题标题】:Conditional launch of android app有条件地启动 android 应用程序
【发布时间】:2016-05-04 09:11:01
【问题描述】:

所以基本上我有一个字符串放在一个文本文件中并保存在我的 SD 卡上。此文本文件包含一个单词“HELLO”

我希望我的 android 应用程序读取此文件中的字符串,如果字符串为“HELLO”,则应用程序应启动,否则应用程序不应启动,而是显示弹出消息,如“未找到正确的字符串”。

这应该在 onCreate 函数中处理吗?并且由于没有分配视图,如何在屏幕上显示应用程序无法启动的消息。 有人可以告诉我如何做到这一点

提前致谢

【问题讨论】:

  • 你搜索过你的问题吗?类似的事情已经有很多答案了。
  • 是的,这是可能的。

标签: android launch


【解决方案1】:

是的,使用此代码可以获得字符串值。然后根据您的限制进行检查:)

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
      sb.append(line).append("\n");
    }
    reader.close();
    return sb.toString();
}

public static String getStringFromFile (String filePath) throws Exception {
    File fl = new File(filePath);
    FileInputStream fin = new FileInputStream(fl);
    String ret = convertStreamToString(fin);
    //Make sure you close all streams.
    fin.close();        
    return ret;
}

【讨论】:

  • 谢谢,能否请告诉我如何显示应用程序无法启动的消息?由于此时没有附加视图,我不知道该怎么做。
  • 嗨 qualitytest 这个网站只是帮助提供一些想法。不要为您的工作提供完整的代码。好的,请做一些谷歌,你可以找到你想要的。
【解决方案2】:

是的,可以先读取在 android 中获取该字符串文件的路径,而不是简单的读取函数读取字符串,如果它是 Hello,而不是应用程序移动得更远,否则它会显示消息和应用程序关闭

【讨论】:

    【解决方案3】:

    这对我有用。

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        //Get text from file
        String ret = "";
        try {
            ret = getStringFromFile("testFile.txt");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String stringTOCheck = "HELLO";
    
        //display a dislogue box if the string does not match
        if (!ret.equals(stringToCheck)) {
    
            AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
            builder1.setMessage("Sorry! This App can't be launched");
    
            //Set to false so that even if the user touches any where else on the screen the dialogue box does not get closed. If you wish it be closed then set to true.
            builder1.setCancelable(false);
    
            builder1.setPositiveButton(
                    "OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                            closeit();
                        }
                    });
    
            AlertDialog alert11 = builder1.create();
            alert11.show();
    
            //Stop the app launch after the user presses ok
            this.finishAffinity();
    
        }
        //Else if string matches continue with normal app launch and execution
        setContentView(R.layout.activity_main);
    
        //other application specific code.
    }
    
    public static String getStringFromFile(String fileName) throws Exception {
    
        //Fetch file from anywhere in the SD card
        File sdcard = Environment.getExternalStorageDirectory();
    
        //Get the file
        File fl = new File(sdcard, fileName);
    
        FileInputStream fin = new FileInputStream(fl);
        String ret = convertStreamToString(fin);
        //Make sure you close all streams.
        fin.close();
        return ret;
    }
    
    public static String convertStreamToString(InputStream is) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        reader.close();
        return sb.toString();
    }
    

    所以基本上条件检查代码应该加在onCreate方法中。 从文件中读取的方法,可以使用 AndroidEnhusiastic 在上一个答案中建议的方法。但正如我的回答中所指出的,我做了一些更改,更具体地解决了这个问题。

    希望这会有所帮助! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      相关资源
      最近更新 更多