Pages

Showing posts with label items. Show all posts
Showing posts with label items. Show all posts

Saturday, April 9, 2016

Threading in Android part 2 Async Tasks

In the previous post we saw one way to deal with threads in Android, which is by using Handlers. In this post well see how to use another technique which is using AsyncTask class.

AsyncTask is an abstract class that provides several methods managing the interaction between the UI thread and the background thread. its implementation is by creating a sub class that extends AsyncTask and implementing the different protected methods it provides.

Lets demonstrate how to user AsyncTask by creating a simple activity that has two buttons and a progress bar:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="vertical"
>
<Button
android_id="@+id/btn"
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_text="Show Progress"
></Button>
<ProgressBar
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"
></ProgressBar>
<Button
android_id="@+id/btnCancel"
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_text="Cancel"
></Button>
</LinearLayout>
Here we have two buttons: one to start progress and the other to stop it.

Creating the AsyncTask sub class:
The first step in implementing AsyncTask is to create a sub class like this:
class ProgressTask extends AsyncTask<Params, Progress, Result>{
}
The AsyncTask declaration has three Varargs parameters which are:
  1. Params: parameter info passed to be used by the AsyncTask.
  2. Progress: the type of progress that the task accomplishes.
  3. The result returned after the AsyncTask finishes.
These parameters are of type Varargs which provide the flexibility to pass dynamic sized arrays as parameters.

In our example our class will be like this:
class ProgressTask extends AsyncTask<Integer, Integer, Void>{
}

The parameter and the progress are of type Integer and the result is Void as our tasks does not return anthing (returns null).
The second step is overriding the protected methods defined by the AsyncTask class that handle the execution life cycle of the AsyncTask.

We have five methods to implement which are:
  1. onPreExecute: the first method called in the AsyncTask, called on the UI thread.
  2. doInBackground: the method that executes the time consuming tasks and publish the task progress, executed in background thread.
  3. onProgressUpdate: method that updates the progress of the AsyncTask, run on the UI thread.
  4. onPostExecute: the final method that gets called after doInBackground finishes, here we can update the UI with the results of the AsyncTask.
  5. onCancelled: gets called if the AsyncTask.cancel() methods is called, terminating the execution of the AsyncTask.

Starting the AsyncTask:
To start the AsyncTask we create an instance of it, then call the execute() method passing the initial parameters like this:
ProgressTask task=new ProgressTask();
// start progress bar with initial progress 10
task.execute(10);
Implementing the AsyncTask:
class ProgressTask extends AsyncTask<Integer, Integer, Void>{

@Override
protected void onPreExecute() {
// initialize the progress bar
// set maximum progress to 100.
progress.setMax(100);

}

@Override
protected void onCancelled() {
// stop the progress
progress.setMax(0);

}

@Override
protected Void doInBackground(Integer... params) {
// get the initial starting value
int start=params[0];
// increment the progress
for(int i=start;i<=100;i+=5){
try {
boolean cancelled=isCancelled();
//if async task is not cancelled, update the progress
if(!cancelled){
publishProgress(i);
SystemClock.sleep(1000);

}

} catch (Exception e) {
Log.e("Error", e.toString());
}

}
return null;
}

@Override
protected void onProgressUpdate(Integer... values) {
// increment progress bar by progress value
progress.setProgress(values[0]);

}

@Override
protected void onPostExecute(Void result) {
// async task finished
Log.v("Progress", "Finished");
}

}

Here are the steps:
  1. onPreExecute() method first gets called initializing the maximum value of the progress bar.
  2. doInBackground(Integer... params) methods gets called by obtaining the initial start value of the progress bar then incrementing the value of the progress bar every second and publishing the progress as long as the async task is not cancelled.
  3. onProgressUpdate(Integer... values) method is called each time progress is published from doInBackground, thus incrementing the progress bar.
  4. onPostExecute(Void result) is called after doInBackground finished execution.
  5. void onCancelled() is called if task.cancel(true) is called from the UI thread. it may interrupt the execution preventing onPostExecute from being executed.

The onClick handler of our buttons is like this:
@Override
public void onClick(View v) {
ProgressTask task=new ProgressTask();
switch(v.getId()){
case R.id.btn:
task.execute(10);
break;
case R.id.btnCancel:
task.cancel(true);
break;
}

}

Read More..

Friday, February 19, 2016

Android Menus part 2 Handling Menu Items Events

We can handle menu items events by three ways:
  1. By implementing onOptionsItemSelected method.
  2. By implementing listeners to single menu items.
  3. By using intents.
Using onOptionsItemSelected method:
Implement the onOptionsItemSelected method like this:
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
TextView txt=(TextView)findViewById(R.id.txt);
txt.setText("you clicked on item "+item.getTitle());
return true;
}

Notice that it returns a Boolean which should be true to execute the method, if false it will not execute.

You can switch between the menu items like this:
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
TextView txt=(TextView)findViewById(R.id.txt);
switch(item.getItemId())
{
case 1:
txt.setText("you clicked on item "+item.getTitle());
return true;
case 2:
txt.setText("you clicked on item "+item.getTitle());
return true;
case 3:
txt.setText("you clicked on item "+item.getTitle());
return true;


}
return super.onOptionsItemSelected(item);

}


Notice that we return true for every handled menu item. And for un handled menu items (outside switch block) we call the super class method.

Using listners:

We can handle options menu items click events by making the activity implement onMenuItemClickListner interface provide an implementation of onMenuItemClick method like this:
public class MenusDemo extends Activity implements OnMenuItemClickListener
then implement the method:
public boolean onMenuItemClick(MenuItem item) {
TextView txt=(TextView)findViewById(R.id.txt);
txt.append("listner");
return false;
}


Notice that the method returns a Boolean. If it returns true no other callbacks will be executed. If returns false then onOptionsItemSelected callback will be executed directly after this callback.

Using intents:
You can specify an intent to be launched when an options menu item is clicked like this:
menu.add(1, “dialItem”, 1, "Dial").setIcon(R.drawable.dvd).setIntent(new Intent(Intent.ACTION_DIAL));

note that if you specify an intent for an item and at the same time override the onOptionsItemSelected method and handle the selection for that item, the precedence of execution is to the code in the onOptionsItemSelected.
meaning that the code in onOptionsItemSelected method will be executed first, if it returns true then the intent will not be launched, if returns false then the intent will launch.

So if you want to use intent for menu items don’t handle it in onOptionsItemSelected method but invoke the parent onOptionsItemSelected.

Scenario 1:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(1, “dialItem”, 1, "Dial").setIcon(R.drawable.dvd).setIntent(new Intent(Intent.ACTION_DIAL)); return super.onCreateOptionsMenu(menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
TextView txt=(TextView)findViewById(R.id.txt);
switch(item.getItemId())
{
case 1:
txt.append("you clicked on item "+item.getTitle());
return true;
case “dialItem”:
txt.setText("you clicked on item "+item.getTitle());
return true;
case 3:
txt.setText("you clicked on item "+item.getTitle());
return true;


}
return super.onOptionsItemSelected(item);

}


This will execute the code of the onOptionsItemSelected method and the dialer intent will not be launched.

Scenario 2:

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(1, “dialItem”, 1, "Dial").setIcon(R.drawable.dvd).setIntent(new Intent(Intent.ACTION_DIAL)); return super.onCreateOptionsMenu(menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
TextView txt=(TextView)findViewById(R.id.txt);
switch(item.getItemId())
{
case 1:
txt.append("you clicked on item "+item.getTitle());
return true;
case “dialItem”:
txt.setText("you clicked on item "+item.getTitle());
return false;
case 3:
txt.setText("you clicked on item "+item.getTitle());
return true;


}
return super.onOptionsItemSelected(item);

}
This will execute the code of the onOptionsItemSelected method and then dialer intent will be launched.


Scenario 3:

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(1, “dialItem”, 1, "Dial").setIcon(R.drawable.dvd).setIntent(new Intent(Intent.ACTION_DIAL)); return super.onCreateOptionsMenu(menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
TextView txt=(TextView)findViewById(R.id.txt);
switch(item.getItemId())
{
case 1:
txt.append("you clicked on item "+item.getTitle());
return true;

case 3:
txt.setText("you clicked on item "+item.getTitle());
return true;


}
return super.onOptionsItemSelected(item);

}



the onOptionsItemSelected does not handle the "dialerItem" selection, so The dialer intent will be launched directly.
Read More..

Monday, February 8, 2016

Dota 2 Hack Items Maps Keys No Survey No Password Free


                                     Dota 2 Hack Items&Maps&Keys 
Today our site freemasterhack.blogspot.ro release Dota 2 hack Tool.We do hard work to make hack to one of the most popular game on Android&iOS.Our Hacks&Cheats and Applications are completly free from viruses.With our Hack you can add Unlimited Coins,Energy and more.Dota 2 Hack Download Tool is avaliable for all Android & iOS devices.Dont wait and Download our Hack Tool and HAVE FUN!!


 If you dont have the game then you can get dota 2 for free. You will be able to get universal map hack for dota 2 which has camera hack and is completely undetectable by the steam client. Not just map hacks but also other awesome cheats like getting the free dota 2 items. Dota 2 items hack will allow you to get the awesome items like, Dragonclaw hook, Golden baby roshan, Immortal, Legendary items and arcana of your favorite hero. You can also enable trade hack feature on our new tool which will enable to trade with anyone without any problem. If you have your treasure or chest locked then do not worry because our new dota 2 treasure key hack allows you to unlock all those treasure with just a single click. Dota 2 wallet hack allows you get get money into your steam account which is complete safe to use. We have also made this compatible for Windows XP, Windows Vista, Windows 7, Windows 7, Linux, Ubuntu and Mac OS and newer version. Our team had made a huge effort on making this tool completely free to use and is also one of the best hack of 2014. Dota 2 Cheats does not contain any kinds of rar passwords, serial key, activation key, verification key or security key on our downloads. If our hack gets some error then you might have to download the net framework in order to work. We will soon take this hack to the next level by bringing you dota 2 online hack. It will allow you to hack whatever you need without downloading anything but for now you have to download the latest dota 2 hack tool.The online tool will be consisting lots of cheats code and commands for PC in the coming future. You can also increase your MMR because of the vision you get from the dota 2 maphack which is also free to download.

                                                          
 Download


Features:

  • You will get free keys, items and wallet
  • Generates unlimited items, keys and wallet
  • Free to download
  • 100% safe to use
  • Easy MMR



Read More..