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




Mar 18, 2011

Using VideoView to play mp4 from sdcard

Using VideoView to play mp4 from sdcard

First of all, download/save a mp4 file in SD Card of the Android device, eg: "/sdcard/Video/Android in Spaaaaaace!_low.mp4".

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<LinearLayout
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   >
<VideoView
   android:id="@+id/myvideoview"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
</LinearLayout>
</LinearLayout>


package com.AnVideoView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class AnVideoView extends Activity {
 
 String SrcPath = "/sdcard/Video/Android in Spaaaaaace!_low.mp4";
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
       myVideoView.setVideoPath(SrcPath);
       myVideoView.setMediaController(new MediaController(this));
       myVideoView.requestFocus();
       myVideoView.start();
   }
}



Related Post:
- A simple example using VideoView to play 3gp from YouTube

Updated:
Using VideoView to play mp4, with MediaController enable/disable

Mar 17, 2011

A simple example using VideoView to play 3gp from YouTube

A simple example using VideoView to play 3gp from YouTube

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<LinearLayout
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
<VideoView
   android:id="@+id/myvideoview"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>


package com.AnVideoView;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class AnVideoView extends Activity {

String SrcPath = "rtsp://v5.cache1.c.youtube.com/CjYLENy73wIaLQnhycnrJQ8qmRMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYPj_hYjnq6uUTQw=/0/0/0/video.3gp";

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
       myVideoView.setVideoURI(Uri.parse(SrcPath));
       myVideoView.setMediaController(new MediaController(this));
       myVideoView.requestFocus();
       myVideoView.start();
   }
}


Related Post:
- Using VideoView to play mp4 from sdcard
- Dual VideoView to play 3gp from YouTube
- How to get 3gp link of Youtube video

Mar 12, 2011

An example of RelativeLayout

An example of XML using RelativeLayout, two buttons are placed on both left and right, and the last button is placed on the center position.

An example of RelativeLayout

<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/leftbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left"
android:layout_alignParentLeft="true"
/>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/leftbutton"
>
<Button
android:id="@+id/rightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right"
android:layout_alignParentRight="true"
/>
<Button
android:id="@+id/centerbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/rightbutton"
android:text="Center Button"
/>
</RelativeLayout>
</RelativeLayout>


Mar 4, 2011

Start Location setting if GPS disabled

The following code can be used to start Location setting:

Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivity(intent);


This example will start Location setting if GPS is curently disabled:
        String GpsProvider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

if(GpsProvider.equals("")){
//GPS Disabled
gpsState.setText("GPS Disable");
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivity(intent);
}else{
//GPS Enabled
gpsState.setText("GPS Enable");
}


Related:
- How to check if GPS is currently enabled or disabled
- Detect GPS ON/OFF status using android.provider.Settings.Secure
- Settings.ACTION_LOCATION_SOURCE_SETTINGS vs. Settings.ACTION_SECURITY_SETTINGS

Mar 3, 2011

How to check if GPS is currently enabled or disabled

Using the method Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED), we can get the allowed location provider, GPS. To check if GPS is currently enabled or disabled, using the code:

        String GpsProvider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

if(GpsProvider.equals("")){
//GPS Disabled
gpsState.setText("GPS Disable");
}else{
//GPS Enabled
gpsState.setText("GPS Enable");
}



Related:
- Start Location setting if GPS disabled
- Detect GPS ON/OFF status using android.provider.Settings.Secure

Infolinks In Text Ads