我正在开发我的第一个应用程序,它从/ data添加对local.prop的调整。 我使用PreferenceActivity实现OnPreferenceChangeListener。 我通过findPreference检测到密钥。
这是代码:
public boolean onPreferenceChange(Preference preference, Object newValue) {
String key = preference.getKey();
if ("first_key".equals(key) && !((CheckBoxPreference) preference).isChecked()) {
// code
}
if ("second_key".equals(key) && !((CheckBoxPreference) preference).isChecked()) {
// code
}
}
因为我添加了第二个if(second_key)所以一切正常。错误是“无法访问的代码”。 请帮我!我做错了什么?
答案 0 :(得分:0)
您尚未添加所有代码。
这不会导致您写的错误。但是,如果您在第二个if之前返回,则可能会出现错误,然后将无法访问。
如果这是一个基于the docs的实现方法,您可能希望在结尾返回true,因此设置了首选项:
public boolean onPreferenceChange(Preference preference, Object newValue) {
String key = preference.getKey();
if ("first_key".equals(key) && !((CheckBoxPreference) preference).isChecked()) {
// Do something if pref 1
} else if ("second_key".equals(key) && !((CheckBoxPreference) preference).isChecked()) {
// Do something if pref 2
}
return true;
}
请注意,您可以在此处使用else if
,因为您不能同时使用这两个键。考虑到已经设置了侦听器,不同的首选项将对此方法进行不同的调用,考虑到您已设置它们。
答案 1 :(得分:0)
package com.fast_gaming.performance;
import java.io.DataOutputStream;
import java.io.File;
import android.content.Context;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.util.Log;
import android.widget.Toast;
public class Main extends PreferenceActivity implements OnPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.main);
findPreference("enable").setOnPreferenceChangeListener(this);
findPreference("debug.sf.nobootanimation").setOnPreferenceChangeListener(this);
}
public boolean onPreferenceChange(Preference preference, Object newValue) {
String key = preference.getKey();
if ("enable".equals(key) && !((CheckBoxPreference) preference).isChecked()) {
try {
Process enable = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(enable.getOutputStream());
outputStream.writeBytes("cd /data\n");
outputStream.flush();
outputStream.writeBytes("touch local.prop\n");
outputStream.flush();
outputStream.writeBytes("chmod 0777 local.prop\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
enable.waitFor();
File file = new File("/system/bin/su");
if (file.exists()) {
Log.d("Main", "This device is rooted!");
return true;
} else {
Log.d("Main", "This device is not rooted!");
Context context = getApplicationContext();
CharSequence text = "This device is not rooted!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
return false;
}
} catch(Exception e) {
return false;
}
} else {
try {
Process disable = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(disable.getOutputStream());
outputStream.writeBytes("cd /data\n");
outputStream.flush();
outputStream.writeBytes("rm local.prop\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
disable.waitFor();
File file = new File("/system/bin/su");
if (file.exists()) {
Log.d("Main", "This device is rooted!");
return true;
} else {
Log.d("Main", "This device is not rooted!");
Context context = getApplicationContext();
CharSequence text = "This device is not rooted!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
return false;
}
} catch (Exception e) {
return false;
}
}
else if ("debug.sf.nobootanimation".equals(key) && !((CheckBoxPreference) preference).isChecked()) {
try {
Process enable1 = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(enable1.getOutputStream());
outputStream.writeBytes("cd /data\n");
outputStream.flush();
outputStream.writeBytes("echo \"debug.sf.nobootanimation=1\" > local.prop\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
enable1.waitFor();
File file = new File("/system/bin/su");
if (file.exists()) {
Log.d("Main", "This device is rooted!");
return true;
} else {
Log.d("Main", "This device is not rooted!");
Context context = getApplicationContext();
CharSequence text = "This device is not rooted!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
return false;
}
} catch (Exception e) {
return false;
}
} else {
try {
Process enable1 = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(enable1.getOutputStream());
outputStream.writeBytes("cd /data\n");
outputStream.flush();
outputStream.writeBytes("sed '/debug..sf.nobootanimation=1/d' filename.txt\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
enable1.waitFor();
File file = new File("/system/bin/su");
if (file.exists()) {
Log.d("Main", "This device is rooted!");
return true;
} else {
Log.d("Main", "This device is not rooted!");
Context context = getApplicationContext();
CharSequence text = "This device is not rooted!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
return false;
}
} catch (Exception e) {
return false;
}
} return true;
}
}
这是完整的代码。我说了什么,什么都没发生。 Eclsipse对我说“令牌上的语法错误”,“删除此令牌”,如果我删除其他,再次“无法访问代码”......