2016-05-04 18 views
0

だから基本的に私は、テキストファイルに配置された文字列を持っているし、私のSDカード上に保存しました。このテキストファイルには「HELLO」という単語が1つ含まれています条件打ち上げ

私のAndroidアプリにこのファイルの文字列を読み込ませ、その文字列が「HELLO」の場合はアプリが起動する必要があります。そうでなければアプリは起動しないでください「正しい文字列が見つかりません」のようなメッセージがポップアップ表示されます。

は、この機能のonCreateで扱われるべきか?そして、何のビューは、アプリを起動することができない、画面にメッセージを表示する方法を割り当てられていないので。それはアプリケーションよりも、こんにちはであれば は、誰かが最初の読み取りは、単純な読み取り機能よりもアンドロイドでその文字列ファイルのパスを取得

+0

はあなたの問題について検索しましたか?似たようなことにはすでに多くの答えがあります。 – Exception

+0

はいこれが可能です。 –

答えて

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によって示唆されているように、ファイルから読み込むための 方法は、同様のものを用いることができます。しかし私は私の答えに示されているように、この問題に特有のいくつかの変更を加えました。

希望すると便利です。 :)

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

ありがとう、私はアプリが起動できないというメッセージをどのように表示できるか教えてください。この時点でビューが添付されていないため、これを行う方法がわかりません。 – qualitytest

+0

こんにちはqualitytestこのサイトはいくつかのアイデアを与えるのに役立ちます。あなたの仕事のための完全なコードを提供していません。 okしてくださいいくつかのGoogleを行うと、あなたが望むものを正確に見つけることができます。 – AndroidEnthusiastic

関連する問題