Skip to content

Instantly share code, notes, and snippets.

@McGalanes
Created January 23, 2017 11:12
Show Gist options
  • Save McGalanes/61a75868372cb923439b2a861ab02c55 to your computer and use it in GitHub Desktop.
Save McGalanes/61a75868372cb923439b2a861ab02c55 to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity {
//Handler & Thread
int mProgression = 0;
Handler mHandler = null;
private static final int WHAT_PROGRESSION = 1;
private boolean mStarted = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHandler = new MyHandler();
findViewById(R.id.buttonStart).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//mServiceName = startService(new Intent(MainActivity.this, TestService.class));
if (!mStarted) {
new Thread(new Runnable() {
@Override
public void run() {
mStarted = true;
mProgression = 0;
while (mProgression < 10) {
try {
mProgression++;
Thread.sleep(1000);
Message message = mHandler.obtainMessage();
message.what = WHAT_PROGRESSION;
message.arg1 = mProgression;
mHandler.sendMessage(message);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Téléchargement terminé !", Toast.LENGTH_SHORT).show();
Log.d("Progression", "completed");
}
});
mStarted = false;
}
}).start();
}
}
});
}
public static class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.d("Progression", msg.arg1 + "/10");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment