Search...

Tuesday, April 24, 2012

How to use TimerTask in Android?



public class Test extends Activity {
    /** Called when the activity is first created. */
Timer myTimer=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        MyTimerTask myTask = new MyTimerTask();
        myTimer = new Timer();
        myTimer.schedule(myTask, 3000, 1500);
       
        Button b=(Button)findViewById(R.id.button1);
     
        b.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

myTimer.cancel();
System.out.println("Cancel the timer...");

}
});
       
     
       
    }
   
   
    class MyTimerTask extends TimerTask {
   public void run() {
   // ERROR
     System.out.println("TimerTask is running......");
   }
  }
}

Friday, April 20, 2012

How to open URL in browser in Android?


        Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url.toString().trim()));
startActivity(i);

How to send email in Android?


    Intent sendIntent = new Intent(Intent.ACTION_SEND);
// Set Address for Email
sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
// Set Subject for Email
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Test");

sendIntent.putExtra(Intent.EXTRA_TEXT, "Expense Category:");

// Attach picture to email
Uri imageFile = Uri.parse("file://" + imagePath);
sendIntent.putExtra(Intent.EXTRA_STREAM, imageFile);

startActivity(Intent.createChooser(sendIntent, "Choose your e-mail"));

Monday, April 2, 2012

How to find the Version of the app in Android?


PackageManager pm = getPackageManager();
 PackageInfo packageInfo = pm.getPackageInfo(this.getPackageName(), 0);
     
 System.out.println("Version Name :"+packageInfo.versionName);
 System.out.println("Version Code :"+packageInfo.versionCode);