get As Int from Bundle - Android Android OS

Android examples for Android OS:Bundle Get

Description

get As Int from Bundle

Demo Code


//package com.book2s;

import android.os.Bundle;

public class Main {
    public static int getAsInt(Bundle b, String key) {
        if (b == null) {
            return 0;
        }/*from  w  w  w. ja  va 2 s . com*/
        if (!b.containsKey(key)) {
            return 0;
        }
        int value = 0;
        String s = b.getString(key);
        if (s == null) {
            try {
                value = b.getInt(key);
            } catch (ClassCastException e) {

            }
        } else {
            try {
                value = Integer.parseInt(s);
            } catch (NumberFormatException e) {

            }
        }
        return value;
    }
}

Related Tutorials