2017-03-08 154 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(); 

    } 


} 

編輯1:

改變if(pinString == response)if(pinString.equals(response))

所以我認爲滿足你的需要。我希望你能幫助我。

回答

0

的問題是這一行:

if(response == pinString) { 

決不比較字符串這樣在Java中。更改爲:

if(pinString.equals(response)) { 

原因是,在Java中,==運算符比較實例,而不是內容。它適用於基元(如int和long),但包括Strings在內的對象需要與.equals()方法進行比較,該方法比較的內容,而不是實例

+0

我試過了,但似乎沒有區別。 – Techassi

+0

你可以在if行上面粘貼它並告訴我它打印的是什麼? Log.i(「StackOverflow」,String.format(「response:%s,pinString:%s」,response,pinString)); – Kelevandos

+0

姆姆我做了,但在我的logcat沒有顯示任何東西。我唯一注意到的是:'03-08 11:39:51.070 23946-23946/com.ts.techassi.ts E/Parcel:讀取一個不支持的NULL字符串。' – Techassi