タイトルからわかるように、必要になるたびに runOnUiThread を使用しないようにしています。どういう意味かは後で説明しますが、最初に、現在のコード (の一部) を次に示します。
private void filePaste()
{
final File src = new File(FileClip); //Source file (as a string)
final File dest = new File(myPath.getText()+"/"+src.getName()); //Destination file
final ProgressDialog dlg = new ProgressDialog(AppContext);
final Thread copythread = new Thread(new Runnable() {
public void run() {
if(FileClip==null)
Main.this.runOnUiThread(new Runnable() {
public void run(){
toast(getString(R.string.msg_EmptyClip));
}
});
else
{
if(src.canRead()){
if(!src.isDirectory())
{
try{
BufferedInputStream in = new BufferedInputStream(new FileInputStream(src), 8192);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
long pos = 0;
long read = 8192;
byte[] data = new byte[(int)read];
while(pos<src.length()){
int bytes = in.read(data, 0, (int)read);
if(bytes>-1) {
out.write(data,0,bytes);
in.skip(read);
in.mark((int)pos+bytes);
in.reset();
dlg.incrementProgressBy(bytes);
pos+=bytes;
}
}
Main.this.runOnUiThread(new Runnable() {
public void run(){
dlg.dismiss();
}
});
in.close();
out.close();
}catch(final Exception e)
{
Main.this.runOnUiThread(new Runnable() {
public void run(){
alert("filePaste():\n"+e);
}
});
}
if(Moving==true) {
boolean q = src.delete();
if(q==true)
Main.this.runOnUiThread(new Runnable() {
public void run(){
toast(getString(R.string.msg_MoveOK,src.getName()));
}
});
else
Main.this.runOnUiThread(new Runnable() {
public void run(){
toast(getString(R.string.msg_MoveNO,src.getName()),1);
}
});
Moving = false;
}
else {
Main.this.runOnUiThread(new Runnable() {
public void run(){
toast(getString(R.string.msg_CopyOK,src.getName()));
}
});
}
}
}
else
Main.this.runOnUiThread(new Runnable() {
public void run(){
toast(getString(R.string.msg_CopyNO,src.getName()));
}
});
FileClip = null;
Main.this.runOnUiThread(new Runnable() {
public void run(){
getDir(myPath.getText().toString());
}
});
}
}
});
dlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dlg.setTitle(Moving?getString(R.string.moving):getString(R.string.copying));
dlg.setMessage(src.getName());
dlg.setCancelable(true);
dlg.setButton(DialogInterface.BUTTON_NEGATIVE, getString(android.R.string.cancel), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
dlg.cancel();
}
});
dlg.setOnCancelListener(new DialogInterface.OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog) {
copythread.interrupt();
if(dest.exists())
dest.delete();
}
});
dlg.setMax((int)src.length());
dlg.show();
copythread.start();
}
このコードが行うことは、指定されたファイル (FileClip に文字列として格納されている) をコピーしようとすることです。
ご覧のとおり、runOnUiThread を過度に使用しているため、コードは非常に長くなります。すべてを移動する方法を知りたい
Main.this.runOnUiThread(new Thread(new Runnable()
public void run()
{
//Simple stuff for 6 lines
}
));
クラスか何かに。考えていた
public class runAtUI implements Runnable
しかし、私はその時点で停止します。コンストラクターの作成方法がわかりません。これで私が何を意味するかを理解していただければ幸いです。何かのようなもの:
new runAtUI()
{
//This makes less lines IMO, and could be executed
}
*注: これに関するトピックを探しましたが、すべて同じで、runOnUiThread を使用してください。私はそれが大丈夫であることを知っていますが、短いもの(同じ目的で toast() を使用してトーストを表示する、またはアラートを再度表示するが、alert() を使用するなど)は無意味です。
PS: これは私が行っている単純なファイル マネージャー用であり、資金不足のため Google Play には公開されません。また、広告も使用したくありません (名前の「単純さ」を破ります)。はい、無料で広告なし (その言葉はありますか?) より優れたアプリがあることは知っていますが、n_n の作成方法を学びたかったのです。
情報、アイデア、またはガイドをいただければ幸いです。
編集#2:迅速に回答してくれたすべての人に感謝します!あなたが提供したサンプルは今のところ機能しますが、もっと柔軟にしたいと思います。eval("code as string")
JavaScript のように (コードが文字列でない場合)。この質問は回答済みと考えますが、人々がさらにアイデアを提供できるように開いたままにします;D
編集 #3: わかりました、最初に、長い間返信がなくて申し訳ありません。次に、現在のコードへのリンクを、現在の 123 行 (このコード) からpastebinの 77 行に追加します。uithreadのコードも追加しています。アプリについて: テスト用に必要な場合は、現在の状態などを確認してください。メールを送ってください。お送りします ;)