我有一个 Java 程序,我刚刚把它变成了一个 slooooooow-to-load Android 应用程序。问题:它处理一个 140,000 字的“字典”(存储在一个Asset
文件中),在其中查找匹配“Windows 通配符”模式的单词:例如,S???CK* 将匹配 STICKS、SHACK、STACK、...、STACKOVERFLOW等。在 Windows 7 中速度非常快。在手机上则不然。
我所做的一件事是将所有 140,000 个单词读入一个ArrayList
(我对它编译并运行感到震惊),之后,只要该模式不以通配符开头,Collections.binarySearch(...)
就可以立即进行查找。
但是将它读入数组列表需要 60 秒,并且用户输入被阻止。并且每次onCreate
都必须运行它——即,不可接受的经常发生。
我想加快速度。
这是一个SSCCE
完美但太慢的方法:
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction
ft;
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.layout_container, new OutputFragment());
ft.commit();
};
}
OutputFragment.java
public class OutputFragment extends Fragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater _layoutInflater,
ViewGroup _sourceOfLayoutParams,
Bundle savedInstanceState)
{
View v = _layoutInflater.inflate(R.layout.fragment_output,_sourceOfLayoutParams, false);
EditText et = (EditText)v.findViewById(R.id.txaOutput);
Matcher matcher = new Matcher(getActivity().getAssets());
for (int i = 0; i < 9; i++)
et.append("\n" + matcher.get(i));
return v;
}
}
Matcher.java
public class Matcher extends ArrayList<String> {
Matcher(AssetManager assets) {
Scanner scDict = null;
try { scDict = new Scanner(assets.open("dictionary.dic")); }
catch (IOException e) { e.printStackTrace(); }
int k = 0;
while(scDict.hasNext())// && ++k<10)
add(scDict.next());
}
}
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width ="match_parent"
android:layout_height ="match_parent"
xmlns:tools ="http://schemas.android.com/tools"
tools:context =".MainActivity"
>
<LinearLayout
android:id ="@+id/layout_container"
android:orientation ="vertical"
android:layout_width ="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</RelativeLayout>
fragment_output.xml
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width ="match_parent"
android:layout_height="match_parent"
android:rowCount="33"
android:columnCount="2">
<TextView
android:id ="@+id/txvOutput"
android:text ="Output shown below"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:textAppearance ="?android:attr/textAppearanceLarge"
android:layout_row="0"
android:layout_column="0">
</TextView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/txaOutput"
android:layout_row="2"
android:inputType="textMultiLine"
android:layout_column="0"
android:maxLines="100"/>
</GridLayout>
所以我想要的是加快速度。我已阅读“让您的 Android 应用程序保持响应”。不知道能不能适应我的情况。我从那里举了一个例子,并尽我所能调整它:
private class LoadWords extends AsyncTask<Scanner, Integer, Long>
{
@Override
protected Long doInBackground(Scanner... params) { // variable arg list required (??)
while(params[0].hasNext()) //// no way this could work...
add(params[0].next());
return 0L;
};
}
我没想到它一输入就可以工作,params[0].hasNext()
但似乎需要一个变量参数列表。
这是我尝试实现它的方法:
LoadWords loadWords = new LoadWords(); /////////////////////////
InputStream stream = null;
Scanner scDict = null;
...
stream = assets.open("dictionary.dic");
...
scDict = new Scanner(stream);
loadWords.execute(scDict); ///////////////////// What should I pass?????
我想我应该放弃这种方法并尝试使用Thread
我必须管理的方法。我对此感到不舒服。
欢迎任何有关如何进行的建议。