Create an object of the task and execute the same task as following.
UpdateCoin updatecoin = new UpdateCoin();
if (updatecoin.getStatus() == AsyncTask.Status.PENDING) {
}
if (updatecoin.getStatus() == AsyncTask.Status.RUNNING) {
}
if (updatecoin.getStatus() == AsyncTask.Status.FINISHED) {
Runnable myRunner = new Runnable() {
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
new UpdateCoin().executeOnExecutor(
AsyncTask.THREAD_POOL_EXECUTOR, coin,
LevelUpBonus[level - 1]);
} else {
new UpdateCoin().executeOnExecutor(
AsyncTask.THREAD_POOL_EXECUTOR, coin,
LevelUpBonus[level - 1]);
}
}
};
myHandler.post(myRunner);
}
Search This Blog
Thursday, August 29, 2013
Tuesday, July 16, 2013
Ecxecute two Different AsyncTask parallel in android
If you want to ecxecute the two different asynctask to be ecxecute parallelly then you shoul use the second async task ecxecution like below. UpdateCoin is the Async task.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
new UpdateCoin().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
mPref.getInt("UserCoin", 0), CalculatedCoin);
} else {
new UpdateCoin().execute(mPref.getInt("UserCoin", 0), CalculatedCoin);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
new UpdateCoin().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
mPref.getInt("UserCoin", 0), CalculatedCoin);
} else {
new UpdateCoin().execute(mPref.getInt("UserCoin", 0), CalculatedCoin);
}
Labels:
AsyncTask,
THREAD_POOL_EXECUTOR
Tuesday, June 4, 2013
Sorting the ArrayList by Name and Value in android
Sorting the ArrayList by String comparison by the Following code.
Collections.sort(name, new Comparator<YOURARRAYLISTNAME>() {
@Override
public int compare(Invitation lhs, Invitation rhs) {
return lhs.getName().compareToIgnoreCase(rhs.getName());
}
});
You can also compare Arraylist by INTEGER value by following code:
Collections.sort(value, new Comparator<YOURARRAYLISTNAME>() {
@Override
public int compare(Invitation lhs, Invitation rhs) {
return (lhs.expenditure < rhs.expenditure ? 1 : -1);
}
});
Collections.sort(name, new Comparator<YOURARRAYLISTNAME>() {
@Override
public int compare(Invitation lhs, Invitation rhs) {
return lhs.getName().compareToIgnoreCase(rhs.getName());
}
});
You can also compare Arraylist by INTEGER value by following code:
Collections.sort(value, new Comparator<YOURARRAYLISTNAME>() {
@Override
public int compare(Invitation lhs, Invitation rhs) {
return (lhs.expenditure < rhs.expenditure ? 1 : -1);
}
});
Labels:
Collections,
Comparator
Thursday, May 23, 2013
Validate the String for the Email
Here is the method for checking the String is email address or not.
private boolean checkemail(String emailstr) {
// TODO Auto-generated method stub
boolean isValid = false;
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = emailstr;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
For validate the String use following:
if(checkemail(emailstr)){
// Here do with the Email string
}else{
System.out.println("email String is not valid");
}
Labels:
ChaeSequence,
Matcher,
Pattern
Thursday, May 9, 2013
Get the number of times app is used
private SharedPreferences mPref;
public static final String PREFS_NAME = "YourPrefName";
int c;
mPref = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
c = mPref.getInt("numRun", 0);
Log.i("TAG", "Number of count of open App:" + c);
c++;
mPref.edit().putInt("numRun", c).commit();
Log.i("TAG", "Number of count after commit of open App:" + c);
Labels:
SharedPreferences
Saturday, April 13, 2013
Count Down Timer in android
Android provide a android.os.CountDownTimer. By new a CountDownTimer object, and overriding the call-back function onTick() and onFinish(), you can implement a count down timer easily.
public class CountDownTimerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView myCounter = (TextView)findViewById(R.id.mycounter);
new CountDownTimer(30000, 1000) {
@Override
public void onFinish() {
// TODO Auto-generated method stub
myCounter.setText("completed Timer!");
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
myCounter.setText("Millisecond Until Finished: " + String.valueOf(millisUntilFinished));
}
}.start();
}
}
Xml file will be below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/mycounter"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
public class CountDownTimerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView myCounter = (TextView)findViewById(R.id.mycounter);
new CountDownTimer(30000, 1000) {
@Override
public void onFinish() {
// TODO Auto-generated method stub
myCounter.setText("completed Timer!");
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
myCounter.setText("Millisecond Until Finished: " + String.valueOf(millisUntilFinished));
}
}.start();
}
}
Xml file will be below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/mycounter"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Labels:
CountDownTimer
Sunday, March 31, 2013
DialogFragment
android.app.DialogFragment, was introduced from API Level 11 (Android 3.0), displays a dialog window, floating on top of its activity's window.
public class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance() {
String title = "My Fragment";
MyDialogFragment f = new MyDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
f.setArguments(args);
return f;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
Dialog myDialog = new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.ic_launcher)
.setTitle(title)
.setPositiveButton("OK",
static MyDialogFragment newInstance() {
String title = "My Fragment";
MyDialogFragment f = new MyDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
f.setArguments(args);
return f;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
Dialog myDialog = new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.ic_launcher)
.setTitle(title)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((AndroidDialogFragmentActivity)getActivity()).okClicked();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((AndroidDialogFragmentActivity)getActivity()).cancelClicked();
}
})
.create();
return myDialog;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/opendialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Dialog" />
</LinearLayout>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonOpenDialog = (Button)findViewById(R.id.opendialog);
buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
OpenDialog();
}});
}
void OpenDialog(){
MyDialogFragment myDialogFragment = MyDialogFragment.newInstance();
myDialogFragment.show(getFragmentManager(), "myDialogFragment");
}
public void okClicked() {
Toast.makeText(AndroidDialogFragmentActivity.this,
"OK Clicked!",
Toast.LENGTH_LONG).show();
}
public void cancelClicked() {
Toast.makeText(AndroidDialogFragmentActivity.this,
"Cancel Clicked!", Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
((AndroidDialogFragmentActivity)getActivity()).okClicked();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((AndroidDialogFragmentActivity)getActivity()).cancelClicked();
}
})
.create();
return myDialog;
}
}
in main.xml modify the following code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/opendialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Dialog" />
</LinearLayout>
now in your activity just change the following code and see the dialog
public class DialogFragmentActivity extends Activity {
/** Called when the activity is first created. */@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonOpenDialog = (Button)findViewById(R.id.opendialog);
buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
OpenDialog();
}});
}
void OpenDialog(){
MyDialogFragment myDialogFragment = MyDialogFragment.newInstance();
myDialogFragment.show(getFragmentManager(), "myDialogFragment");
}
public void okClicked() {
Toast.makeText(AndroidDialogFragmentActivity.this,
"OK Clicked!",
Toast.LENGTH_LONG).show();
}
public void cancelClicked() {
Toast.makeText(AndroidDialogFragmentActivity.this,
"Cancel Clicked!", Toast.LENGTH_LONG).show();
}
}
Labels:
Alert Dialog,
Bundle,
Dialog,
DialogFragment,
Toast
Subscribe to:
Posts (Atom)