有条件地启动 android 个应用

Conditional launch of android app

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

我想让我的 android 应用程序读取此文件中的字符串,如果字符串是 "HELLO" 应用程序应该启动,否则应用程序不应启动,而是显示一条弹出消息像 "Correct string not found".

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

提前致谢

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

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

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;
}

这是对我有用的方法。

@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方法中添加条件检查代码。 从文件中读取的方法,可以按照 AndroidEnthusiastic 在上一个答案中的建议使用。但是我已经按照我的回答中的指示进行了一些更改,更具体地针对这个问题。

希望对您有所帮助! :)