Pages

Monday, 16 September 2013

Android notification using notification builder

To work following code for android version before 3.0, You need to add android support library.
Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
        YOUR_PI_REQ_CODE, notificationIntent,
        PendingIntent.FLAG_CANCEL_CURRENT);

NotificationManager nm = (NotificationManager) ctx
        .getSystemService(Context.NOTIFICATION_SERVICE);

Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);

builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.some_img)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
            .setTicker(res.getString(R.string.your_ticker))
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle(res.getString(R.string.your_notif_title))
            .setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();

nm.notify(YOUR_NOTIF_ID, n);

Sunday, 15 September 2013

android string to date or date to string format


public class DateFormatClass {
    public static void main(String[] args) {

        String mytime="Jan 17, 2012";
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "MMM dd, yyyy");
        Date myDate = null;
        try {
            myDate = dateFormat.parse(mytime);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
        String finalDate = timeFormat.format(myDate);

        System.out.println(finalDate);
    }
}

Thursday, 22 August 2013

Android how to check earphone plugged in or not

Pls check following code to check earphone status

need to register broadcast receiver in activity


private BroadcastReceiver earPhoneStateReceiver = new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {
   if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {

    int state = intent.getIntExtra("state", -1);
    switch (state) {
    case 0:
     
     Log.d(TAG, "Headset is unplugged");
     break;
    case 1:
     
     Log.d(TAG, "Headset is plugged");
     break;
    default:
     
     Log.d(TAG, "I have no idea what the headset state is");
    }
   }
  }
 };


in On create of activity need to register above receiver
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.detail);


  // register receiver for ear phone
  IntentFilter receiverFilter = new IntentFilter(
    Intent.ACTION_HEADSET_PLUG);

  registerReceiver(earPhoneStateReceiver, receiverFilter);
  // end register receiver for ear phone

}

Tuesday, 13 August 2013

Android how to mix 2 wav audio?

Basic steps to mix 2 wav files.
  • just take byte[] (or amplitude array is also good) and from both file..
  • calculate average of every byte and prepare new array
  • make wave header and set new file's header
  • then write byte array to new file
  • then try to play using MediaPlayer (MediaPlayer will give great result when play sound then audio track)
Click here to download demo.
Following are condition in above demo.You can change them as per requirement.

Low Song format must have
we have to upload song in WAV format with following values.

song formats in demo are 
1.  Format 1 param : 
     Freq : 11.025 kHz,
     bit per sample :  8 Bit,
     channel mode :  Mono

2.  Format 2 param : 
     Freq : 44100 kHz,
     bit per sample :  8 Bit,
     channel mode :  Mono


How to convert mp3 to wav with different options :
These can be done using Free software. 
Software Download url : http://www.koyotesoft.com/audio-software/free-mp3-wma-converter.html

Friday, 19 July 2013

Android - Convert String to valid URL

Just copy-paste below code and call the function with your string url as param.
private URL ConvertToUrl(String urlStr) {
 try {
  URL url = new URL(urlStr);
  URI uri = new URI(url.getProtocol(), url.getUserInfo(),
    url.getHost(), url.getPort(), url.getPath(),
    url.getQuery(), url.getRef());
  url = uri.toURL();
  return url;
 } catch (Exception e) {
  e.printStackTrace();
 }
 return null;
}