有条件的启动Android应用程序

时间:2016-05-04 09:11:02

标签: android launch

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

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

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

提前致谢

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

是的,有可能首先读取获取android中该字符串文件的路径而不是简单的读取函数读取字符串如果它是Hello比应用程序进一步移动其他它显示消息和应用程序关闭

答案 2 :(得分:0)

这对我有用。

@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在上一个答案中建议的方法。但是我在答案中指出了一些更改,更具体地说明了这个问题。

希望这有帮助! :)