import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
android.content.Context _this=this;
boolean thread_run;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thread_run=true;
Thread th=new Thread(new test());
th.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
thread_run=false;
}
android.app.ProgressDialog pd;
private class test implements Runnable {
android.os.Handler handler=new android.os.Handler();
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
pd = new android.app.ProgressDialog(_this);
pd.setTitle("title");
pd.setMessage("message");
pd.setProgressStyle(android.app.ProgressDialog.STYLE_SPINNER);
pd.setCancelable(false);//ダイアログの外をクリックされた場合でも消えないようにする
pd.show();
}
});
try {
Thread.sleep(10000);
} catch (Exception e) {}
if(false==thread_run) return;
handler.post(new Runnable() {
@Override
public void run() {
pd.dismiss();//ダイアログを閉じる
}
});
}
}
}
上のコードを実行すると次のようなグルグル回るダイアログが表示されます。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
android.content.Context _this=this;
boolean thread_run;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thread_run=true;
Thread th=new Thread(new test());
th.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
thread_run=false;
}
android.app.ProgressDialog pd;
private class test implements Runnable {
android.os.Handler handler=new android.os.Handler();
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
pd = new android.app.ProgressDialog(_this);
pd.setTitle("title");
pd.setMessage("message");
pd.setProgressStyle(android.app.ProgressDialog.STYLE_HORIZONTAL);
pd.setCancelable(false);//ダイアログの外をクリックされた場合でも消えないようにする
pd.setMax(100);//最大値を設定
pd.setProgressNumberFormat("%1d / %2d Byte");
pd.show();
}
});
for(int i=0;i<100;i++) {
try {
Thread.sleep(100);
} catch (Exception e) {}
if(false==thread_run) return;
handler.post(new Runnable() {
@Override
public void run() {
pd.incrementProgressBy(1);//現在の値に加算、加算される値のため注意
}
});
}
handler.post(new Runnable() {
@Override
public void run() {
pd.dismiss();
}
});
}
}
}


