Reputation: 1514
I have an AlertDialog builder in class. I am setting some message into it which comes from reading a file. Earlier as file text wasn't too large it use to load easily, now since the text has grown more it takes a time to load dialog and blocks UI. How can i run this in thread ?
Edited code :
public class Eula TaskCompleteListner{ {
static interface OnEulaAgreedTo {
void onEulaAgreedTo();
}
public static boolean show(final Activity activity,final Context context,final Boolean flag) {
final Preferences prefs = Preferences.getInstance();
Log.d(TAG, "insideEula");
if (!prefs.getEulaStatus(context)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(
activity);
Log.d(TAG, "insideEulaLaunch");
builder.setTitle(R.string.eula_title);
builder.setCancelable(true);
builder.setPositiveButton(R.string.eula_accept,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
prefs.setEulaStatus(context, true);
if (activity instanceof OnEulaAgreedTo) {
((OnEulaAgreedTo) activity).onEulaAgreedTo();
}
}
});
builder.setNegativeButton(R.string.eula_refuse,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
refuse(activity);
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
refuse(activity);
}
});
MyAsync async= new MyAsync(activity, new TaskCompleteListner() {
public boolean onComplete(String result) {
builder.setMessage(data);
builder.create().show();
return false;
}
}) ;
MyAsync async= new MyAsync(this, activity) ;
async.excecute();
//builder.setMessage(readEula(activity)); //READING FILE AND SETTING HERE
//builder.create().show();
return false;
}
return true;
}
private static void refuse(Activity activity) {
activity.finish();
}
@Override
public boolean onComplete(String result) {
// TODO Auto-generated method stub
builder.setMessage(readEula(activity)); //READING FILE AND SETTING HERE
builder.create().show();
return false;
}
Async Task Class
public class MyAsync extends AsyncTask<Void, Void, String>{
public static final String ASSET_EULA = "EULA";
TaskCompleteListner taskCompleteListner;
Activity activity;
public interface TaskCompleteListner{
public boolean onComplete(String result);
}
public MyAsync(TaskCompleteListner taskCompleteListner,Activity activity) {
this.taskCompleteListner = taskCompleteListner;
this.activity=activity;
}
@Override
protected String doInBackground(Void... params) {
String data=(String)readEula(activity);
return data;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
taskCompleteListner.onComplete(result);
}
private static CharSequence readEula(Activity activity) { //READING FILE HERE
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(activity.getAssets().open(ASSET_EULA)));
String line;
StringBuilder buffer = new StringBuilder();
while ((line = in.readLine()) != null)
buffer.append(line).append('\n');
byte[] latin1 = buffer.toString().getBytes("ISO-8859-1");
return new String(latin1);
//return buffer;
} catch (IOException e) {
return "";
} finally {
closeStream(in);
}
}
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// Ignore
}
}
}
}
Upvotes: 0
Views: 401
Reputation: 892
You can use AsyncTask
class, where you read your data in doInBackground()
return the CharSequence
and do the dialog.show() in onPostExecute()
.
EDIT:
heres what you can do,
create a class
private static class MyAsyncClass extends AsyncTask<Void,Void,CharSequence > {
Activity activity;
ProgressDialog dialog
public MyAsyncClass(Activity activity){
this.activity = activity;
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(activity);
dialog.setMessage("Reading data");
dialog.setCancelable(false);
dialog.show();
}
@Override
protected CharSequence doInBackground(Void... params) {
return readEula(activity);
}
protected void onPostExecute(CharSequence data) {
if(dialog!=null && dialog.isShowing())
dialog.dismiss();
final AlertDialog.Builder builder = new AlertDialog.Builder(
activity);
Log.d(TAG, "insideEulaLaunch");
builder.setTitle(R.string.eula_title);
builder.setCancelable(true);
builder.setPositiveButton(R.string.eula_accept,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
prefs.setEulaStatus(context, true);
if (activity instanceof OnEulaAgreedTo) {
((OnEulaAgreedTo) activity).onEulaAgreedTo();
}
}
});
builder.setNegativeButton(R.string.eula_refuse,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
refuse(activity);
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
refuse(activity);
}
});
builder.setMessage(data);
builder.create().show();
}
}
then call this class as,
if (!prefs.getEulaStatus(context)) {
MyAsyncClass myAsyncClass = new MyAsyncClass(activity);
myAsyncClass.execute();
}
Correction to your Edit:
in your Eula class,
change this,
MyAsync async= new MyAsync(activity, new TaskCompleteListner() { public boolean onComplete(String result) { builder.setMessage(data); builder.create().show(); return false; } }) ;
MyAsync async= new MyAsync(this, activity) ;
async.excecute();
to this,
MyAsync async= new MyAsync(activity, new TaskCompleteListner() {
public boolean onComplete(String result) {
builder.setMessage(data);
builder.create().show();
return false;
}
}) ;
async.excecute();
in your Async class,
change your constructor to,
public MyAsync(Activity activity, TaskCompleteListner taskCompleteListner) { this.taskCompleteListner = taskCompleteListner; this.activity=activity; }
Upvotes: 3
Reputation: 2882
Use this Async Class to get the text
public class MyAsync extends AsyncTask<Void, Void, String>{
TaskCompleteListner taskCompleteListner;
Activity activity;
public interface TaskCompleteListner{
public boolean onComplete(String result);
}
public MyAsync(TaskCompleteListner taskCompleteListner,Activity activity) {
this.taskCompleteListner = taskCompleteListner;
this.activity=activity;
}
@Override
protected String doInBackground(Void... params) {
String data=(String) readEula(activity);
return data;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
taskCompleteListner.onComplete(result);
}
private static CharSequence readEula(Activity activity) { //READING FILE HERE
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(activity.getAssets().open(ASSET_EULA)));
String line;
StringBuilder buffer = new StringBuilder();
while ((line = in.readLine()) != null)
buffer.append(line).append('\n');
byte[] latin1 = buffer.toString().getBytes("ISO-8859-1");
return new String(latin1);
//return buffer;
} catch (IOException e) {
return "";
} finally {
closeStream(in);
}
}
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// Ignore
}
}
}
}
You can use this in your Eula class as follows:
if (!prefs.getEulaStatus(context)) {
MyAsync async= new MyAsync(activity,new TaskCompleteListner() {
@Override
public boolean onComplete(String result) {
//TODO show your alert dialog here. Result has the string needed
return false;
}
}) ;
}
Hope this helps.
Upvotes: 1
Reputation: 304
Async task will be the better approach. 1. Do your background operation (readEula(Activity activity)) in doInBackGround and 2. show dialog in onPostExecute method.
In another approach create thread and do your operation (readEula(act)) in it and use handler to communicate to this thread and show alert dialog in you activity only.
Upvotes: 0
Reputation: 669
If its just a dialog u need to show, you can use the Activity's following method:
public final void runOnUiThread (Runnable action)
AsyncTask would be a cleaner approach. However, this will save you the trouble of extra code if you are looking for a quick switch onto the main thread.
Upvotes: 0