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);

Saturday, March 24, 2012

How to know when TTS is finished in Android?


public class TTSActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener, ... {
private TextToSpeech mTts;
...........
private void speak(String text) {
   if(text != null) {
      HashMap<String, String> myHashAlarm = new HashMap<String, String>();
      myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
      myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "SOME MESSAGE");
      mTts.speak(text, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
   }
}
// Fired after TTS initialization
public void onInit(int status) {
    if(status == TextToSpeech.SUCCESS) {
        mTts.setOnUtteranceCompletedListener(this);
    }
}
// It's callback
public void onUtteranceCompleted(String utteranceId) {
   Log.i(TAG, utteranceId); //utteranceId == "SOME MESSAGE"
   }
...........
}

Tuesday, March 20, 2012

How to read contacts on Android ?


First, ensure that you have added android.permission.READ_CONTACTS to your AndroidManifest.xml file, then you can loop through your phone contacts like this:


Code:

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
   String contactId = cursor.getString(cursor.getColumnIndex(
   ContactsContract.Contacts._ID));
   String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
   if (Boolean.parseBoolean(hasPhone)) {
      // You know it has a number so now query it like this
      Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
      while (phones.moveToNext()) {
         String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));                
      }
   phones.close();
}

Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
   // This would allow you get several email addresses
   String emailAddress = emails.getString(
   emails.getColumnIndex(ContactsContract.CommonDataKinds.CommonDataColumns.DATA));
}
emails.close();
cursor.close();