qualitytest
qualitytest

Reputation: 833

Conditional launch of android app

So basically I have a string placed in a text file and kept on my sd card. This text file contains a single word "HELLO"

I want my android app to read the string in this file, and if the string is "HELLO" the app should launch, otherwise the app should not launch and instead show a pop up message like "Correct string not found".

Should this be handled in the onCreate function? And since no view is assigned how to display message on the screen that the app can not be launched. Could someone please tell me how to do this

Thanks in advance

Upvotes: 0

Views: 227

Answers (3)

qualitytest
qualitytest

Reputation: 833

Here is what worked for me.

@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();
}

So basically the condition check code should be added in the onCreate method. Methods to read from file, the same can be used as suggested by AndroidEnthusiastic in the previous answer. But I have made a few changes as indicated in my answer, more specific to this problem.

Hope this helps! :)

Upvotes: 0

AndroidEnthusiastic
AndroidEnthusiastic

Reputation: 931

Yes it is possible by using this code you can get the string value. then check based on your constraints:)

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

Upvotes: 1

bhupinder singh
bhupinder singh

Reputation: 3

yes it is possible first read get the path of that string file in android than a simple read function read the string if it is Hello than application move further else it shows message and application close

Upvotes: 0

Related Questions