0
showDialog()でメッセージを表示する方法。ダウンロード表示してポップアップメッセージを表示
freshDownloadViewが終了したらshow showDialogを表示します。次のように
私のコードは次のとおりです。
public class MainActivity extends Activity implements View.OnClickListener {
private FreshDownloadView freshDownloadView;
private Button btDownloaded;
private TextView btReset;
private TextView btDownloadError;
private final int FLAG_SHOW_OK = 10;
private final int FLAG_SHOW_ERROR = 11;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int progress = (int) msg.obj;
freshDownloadView.upDateProgress(progress);
switch (msg.what) {
case FLAG_SHOW_OK:
break;
case FLAG_SHOW_ERROR:
freshDownloadView.showDownloadError();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
freshDownloadView = (FreshDownloadView) findViewById(R.id.pitt);
btDownloaded = (Button) findViewById(R.id.bt_downloaded);
btReset = (Button) findViewById(R.id.bt_reset);
btDownloadError = (Button) findViewById(R.id.bt_download_error);
btDownloaded.setOnClickListener(this);
btReset.setOnClickListener(this);
btDownloadError.setOnClickListener(this);
}
これは、私はあなたがこのためにAsyncTask
を使用することをお勧めボタンfreshDownload
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_downloaded:
if (freshDownloadView.using()) return;
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message message = Message.obtain();
message.obj = i;
handler.sendMessage(message);
showDialog();
}
}
}).start();
break;
case R.id.bt_reset:
freshDownloadView.reset();
break;
case R.id.bt_download_error:
if (freshDownloadView.using()) return;
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 30; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message message = Message.obtain();
if (i == 30) {
message.what = FLAG_SHOW_ERROR;
}
message.obj = i;
handler.sendMessage(message);
}
}
}).start();
break;
}
}
public void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle(R.string.dialog_title)
.setMessage(R.string.download_app)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.create().show();
}
protected void onPause() {
// unregisterReceiver(receiver);
super.onPause();
}
protected void onResume() {
// registerReceiver(receiver, new IntentFilter(
// WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
}