我尝试将PIN输入作为对话框。 这部分工作正常。但是当我输入我的引脚并尝试将其与我所请求的引脚进行比较时,它就会关闭我的对话框。
所以这是我的代码: 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 :(得分:0)
问题在于这一行:
if( response == pinString) {
永远不要在Java中比较这样的字符串。将其更改为:
if( pinString.equals(response) ) {
原因是,在Java中,==运算符比较实例,而不是内容。它适用于基元(如整数和长整数),但对象(包括字符串)需要与.equals()方法进行比较,该方法比较内容,而不是实例。