Mar 30, 2011

Detect Key action

To detect key action of Android device from user, you can override the methods onKeyDown() and onKeyUp().

Example:
package com.AndroidKey;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

public class AndroidKey extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub

switch(keyCode){
case KeyEvent.KEYCODE_MENU:
Toast.makeText(AndroidKey.this, "KEYCODE_MENU", Toast.LENGTH_LONG).show();
return true;
case KeyEvent.KEYCODE_SEARCH:
Toast.makeText(AndroidKey.this, "KEYCODE_SEARCH", Toast.LENGTH_LONG).show();
return true;
//...more case...
default:
return super.onKeyDown(keyCode, event);
}
}
}


Mar 29, 2011

Pass data from activity to service

In activity:
 Intent myIntent = new Intent(MainActivity.this, MyService.class);

Bundle bundle = new Bundle();
bundle.putCharSequence("extraData", data);
myIntent.putExtras(bundle);

pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);

Basically, it's similar to that in "Pass data between activity".

In service side, there are no getIntent().getExtras() in Service class. We can handle it in onStart() call-back method, intent will be passed as parameter.
 public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);

Bundle bundle = intent.getExtras();
data = (String) bundle.getCharSequence("extraData");
smsTextToSend = (String) bundle.getCharSequence("extraSmsText");
...
}

Mar 27, 2011

Pass data between activity

When start a new activity in our main activity using startActivity(), data can be passed using Bundle.

Inside the main activity, MainActivity.java
            Intent intent = new Intent();
intent.setClass(MainActivity.this, NewActivity.class);

Bundle bundle = new Bundle();
bundle.putCharSequence("extraCharSequence", "hello");
bundle.putInt("extraInt", 12345);
//...
intent.putExtras(bundle);
startActivity(intent);


Get the data in onCreate() of the new activity, NewActivity.java
    public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Bundle bundle = this.getIntent().getExtras();
CharSequence dataCharSequence = bundle.getCharSequence("extraCharSequence");
int dataInt = bundle.getCharSequence("extraInt");


};


The code this.getIntent().getExtras() return the map of all extras previously added with putExtra(), or null if none have been added.

Mar 23, 2011

Send email by startActivity with Intent.ACTION_SEND

Here is a example by calling startActivity() with Intent.ACTION_SEND. Android OS will start a app Chooser suitable for "plain/text" email sending.

    String emailList[] = {emailAddress1, emailAddress2, ...};

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, emailList);
intent.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Email Text");
startActivity(Intent.createChooser(intent, "Choice email App"));


Mar 22, 2011

Send SMS by startActivity with Intent.ACTION_SENDTO

Here is a example by calling startActivity() with Intent.ACTION_SENDTO. Android default SMS app will be started to send SMS with pre-defined text and number.

remark: change "xxxxxxxx" to the number of the phone to receive SMS.

        Uri uri = Uri.parse("smsto:xxxxxxxx");  
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "SMS text");
startActivity(intent);




Infolinks In Text Ads