簡體   English   中英

有條件地啟動android應用

[英]Conditional launch of android app

因此,基本上,我在文本文件中放置了一個字符串,並將其保留在我的SD卡中。 該文本文件包含一個單詞“ HELLO”

我希望我的Android應用程序讀取此文件中的字符串,如果字符串為“ HELLO”,則應啟動該應用程序,否則,該應用程序不應啟動,而是顯示諸如“找不到正確的字符串”之類的彈出消息。

是否應該在onCreate函數中處理? 而且由於未分配任何視圖,因此如何在屏幕上顯示無法啟動該應用程序的消息。 有人可以告訴我該怎么做嗎

提前致謝

是的,可以通過使用此代碼來獲取字符串值。 然后根據您的限制進行檢查:)

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

是的,它可能比Android的簡單讀取功能先讀取獲取該字符串文件的路徑。

這對我有用。

@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在前面的答案中建議的方法相同的方法。 但是我做了一些修改,如我的回答所示,更具體地針對此問題。

希望這可以幫助! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM