2017-03-08 7 views
0

PIN入力をダイアログとして実行しようとしています。 この部分は正常に動作しています。しかし、私がピンを入力してそれを私の要求されたピンと比較しようとすると、私のダイアログが閉じられるだけです。だからここPIN入力のあるAndroidダイアログボックス

が私のコードです: PinInput.java

package com.ts.techassi.ts; 

import android.app.AlertDialog; 
import android.app.Dialog; 
import android.app.DialogFragment; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.pm.PackageManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.widget.EditText; 
import android.widget.TextView; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 


public class PinInput extends DialogFragment { 
    public final static String EXTRA_MESSAGE = "tuerschild.saschalautenschlaege.tuerschild.MESSAGE"; 
    Context context; 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     LayoutInflater inflater = getActivity().getLayoutInflater(); 
     builder.setView(inflater.inflate(R.layout.pin_input, null)); 
     Log.i("LOL", "Started"); 

     builder.setMessage("Enter PIN") 
       .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         Uri url = readUrl(); 
         Dialog f = (Dialog) dialog; 
         // String ts_id = url.getLastPathSegment(); 
         EditText pin = (EditText)f.findViewById(R.id.pin); 
         TextView test = (TextView) f.findViewById(R.id.response); 
         String pinString = pin.getText().toString(); 

         try { 
          String response = new Http().execute().get(); 
          // Log.i("test", response); 
          if(response == pinString) { 
           Boolean isLauncher = isMyAppLauncherDefault(); 
           if(isLauncher == true){ 
            resetPreferredLauncherAndOpenChooser(context); 
           } 
          } else { 
           PinInput.this.getDialog().cancel(); 
          } 
         } catch (Exception e) { 
          e.printStackTrace(); 
          Log.i("Test",e.toString()); 
         } 
        } 
       }) 
       .setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // User cancelled the dialog 
        } 
       }); 
     return builder.create(); 
    } 

    public Uri readUrl() { 
     String fileDirectory = Environment.getExternalStorageDirectory()+"/dbDoorsign/url.txt"; 

     File file = new File(fileDirectory); 

     StringBuilder text = new StringBuilder(); 
     ; 

     try { 
      BufferedReader br = new BufferedReader(new FileReader(file)); 
      String line; 

      while ((line = br.readLine()) != null) { 
       text.append(line); 
      } 
      br.close(); 
     } 
     catch (IOException e) { 

     } 
     return Uri.parse(text.toString()); 
    } 

    private boolean isMyAppLauncherDefault() { 
     final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN); 
     filter.addCategory(Intent.CATEGORY_HOME); 

     List<IntentFilter> filters = new ArrayList<IntentFilter>(); 
     filters.add(filter); 

     final String myPackageName = getActivity().getPackageName(); 
     List<ComponentName> activities = new ArrayList<ComponentName>(); 
     final PackageManager packageManager = (PackageManager) getActivity().getPackageManager(); 

     packageManager.getPreferredActivities(filters, activities, null); 

     for (ComponentName activity : activities) { 
      if (myPackageName.equals(activity.getPackageName())) { 
       return true; 
      } 
     } 
     return false; 
    } 

    public static void resetPreferredLauncherAndOpenChooser(Context context) { 
     PackageManager packageManager = context.getPackageManager(); 
     ComponentName componentName = new ComponentName(context, FakeLauncherActivity.class); 
     packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); 

     Intent selector = new Intent(Intent.ACTION_MAIN); 
     selector.addCategory(Intent.CATEGORY_HOME); 
     selector.putExtra(EXTRA_MESSAGE, "1"); 
     selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(selector); 

     packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP); 
    } 

} 

私の第二のファイルHttp.java

package com.ts.techassi.ts; 

import android.os.AsyncTask; 
import android.widget.TextView; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 


public class Http extends AsyncTask<String, Void, String> { 
    String USER_AGENT = "Mozilla/5.0"; 
    private TextView scanResults; 

    @Override 
    protected String doInBackground(String... url) { 

     try { 
      return sendGet(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return e.toString(); 
     } 
    } 

    private String sendGet() throws Exception { 

     String url = "http://my.url.com"; 

     URL obj = new URL(url); 
     HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

     // optional default is GET 
     con.setRequestMethod("GET"); 

     //add request header 
     con.setRequestProperty("User-Agent", USER_AGENT); 

     int responseCode = con.getResponseCode(); 

     BufferedReader in = new BufferedReader(
       new InputStreamReader(con.getInputStream())); 
     String inputLine; 
     StringBuffer response = new StringBuffer(); 

     while ((inputLine = in.readLine()) != null) { 
      response.append(inputLine); 
     } 
     in.close(); 

     // print result 
     // System.out.println(response.toString()); 
     return response.toString(); 

    } 


} 

EDIT 1:

if(pinString.equals(response))

if(pinString == response)を変更だから私はそれを考えているあなたが必要とするものすべてが必要です。あなたが私を助けることを願っています。

答えて

0

問題は、この行です:

if(response == pinString) { 

は、Javaでのこのような文字列を比較することはありません。その理由は、Javaでは、==演算子がインスタンスではなく内容を比較するためです。これは、(int型とlong型など)プリミティブのために動作しますが、文字列を含むオブジェクトは、内容を比較.equals()メソッドではなく、インスタンスと比較する必要があります。

+0

私はそれを試しましたが、違いはないようです。 – Techassi

+0

これをif行の上に貼り付けて、何を印刷するのか教えてください。 Log.i( "StackOverflow"、String.format( "レスポンス:%s、ピンストリング:%s"、レスポンス、ピンストリング)); – Kelevandos

+0

私はしましたが、私のログカットには何も表示されていません。私が気がついたのは、03-08 11:39:51.070 23946-23946/com.ts.techassi.ts E/Parcel:ここではサポートされていないNULL文字列を読み込んでいます。 ' – Techassi

関連する問題