Using 'Async Tasks' on Android to generate random number and output
I am writing an random number generator program to show the output every sec.
The overview is that every second when the random number change I would want to random number to be output to the 'EditText' box. Is it possible?
I have research on it but result in unsuccessfully (mainly because of how the async works).
This is the code that I have written:
package com.example;
import java.util.Random;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AsyncTasksActivity extends Activity {
EditText txtMessage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String ranString = "";
try{
ranString = "";
for(int a = 0; a < 33; a++)
{
Random rn = new Random();
int i = rn.nextInt(256);
ranString = ranString + i + " ";
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
txtMessage = (EditText) findViewById(R.id.txtMessage);
txtMessage.setText(ranString);
}
public class runRandomly extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
}
}
In case you need to run the code to ensure it works, I have place the xml code here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView开发者_运维技巧
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Null"
/>
<EditText
android:id="@+id/Null"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:inputType="numberDecimal"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Random Key"/>
<EditText
android:id="@+id/txtMessage"
android:layout_width="fill_parent"
android:gravity="center"
android:editable="false" android:layout_height="61dp" android:layout_weight="0.14"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Much Thanks!
From my point of view,
Make a constructor of your AsyncTask class passing activity signature, like,
public runRandomly(Activity activity) { this.activity = activity; context = activity; }
also override this method
@Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); if (values[0] == 0) { mDialog.setTitle("Processing..."); } mDialog.setProgress(values[0]); }
update the onProgressUpdate(call it) from your doInBackground(String... params) method
EDIT: you can also give a reference of your EditText view in AsyncTask constructor and do update in onProgressUpdate method.
EDIT: use this code..
public class NewClass extends Activity {
/** Called when the activity is first created. */
EditText edt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newclass);
edt=(EditText)findViewById(R.id.ed);
new runRandomly(this,edt).execute();
}
public class runRandomly extends AsyncTask<String, String, String> {
String ranString="";
Activity activity;
EditText edtt;
public runRandomly(Activity activity,EditText edit) {
this.activity = activity;
edtt=edit;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
for(int a = 0; a < 33; a++)
{
Random rn = new Random();
int i = rn.nextInt(256);
ranString = ranString + i + " ";
}
onProgressUpdate();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
edtt.setText(""+ranString);
super.onProgressUpdate(values);
}
}
}
Simple, Thanx.
put this line outside of the for loop it will re-generate new object so no need it in loop
Random rn = new Random();
for(int a = 0; a < 33; a++)
{
int i = rn.nextInt(256);
ranString = ranString + i + " ";
}
you need to Handler and Runnable object which call the AsynTask execute method using runRandomly object
like create this object
Handler handler = new Handler();
runRandomly randomly = new runRandomly();
Runnable callRandom = new Runnable(){
public void run(){
//// call the execute method
randomly.execute();
///// here handler call this runnable every 1 sec i.e 1000 delay time
handler.postDelay(callRandom,1000);
}
}
in onBackground generate the number @Override protected String doInBackground(String... params) { String ranString = "";
Random rn = new Random();
for(int a = 0; a < 33; a++)
{
int i = rn.nextInt(256);
ranString = ranString + i + " ";
}
return ranString;
}
this above method will generate the random number string and when it finish this will return that string to the postExecute();
@Override
protected void onProgressUpdate(String values) {
super.onProgressUpdate(values);
if(values!=null & !values.equals(""))
txtMessage.setText(ranString);
}
精彩评论