Android How to - Intercept Parameter from Intent








Question

We would like to know how to intercept Parameter from Intent.

Answer

/* w  w w  . j  a  v  a2  s. co m*/
/*
 * Copyright (C) 2012-2014 Ognyan Bankov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//package com.github.khandroid.misc;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

public class Main{
    public static long interceptLongParam(Bundle savedInstanceState,
                                          Intent intent, String paramName) {
        long ret = 0;

        if (savedInstanceState != null) {
            ret = savedInstanceState.getLong(paramName);
        } else {
            if (intent != null) {
                Bundle incomming = intent.getExtras();
                if (incomming != null) {
                    ret = incomming.getLong(paramName);
                }
            }
        }

        return ret;
    }


    public static Parcelable interceptParcelableParam(
                                                      Bundle savedInstanceState,
                                                      Intent intent,
                                                      String paramName) {
        Parcelable ret = null;

        if (savedInstanceState != null) {
            ret = savedInstanceState.getParcelable(paramName);
        } else {
            if (intent != null) {
                Bundle incomming = intent.getExtras();
                if (incomming != null) {
                    ret = incomming.getParcelable(paramName);
                }
            }
        }

        return ret;
    }


    public static String interceptStringParam(Bundle savedInstanceState,
                                              Intent intent, String paramName) {
        String ret = null;

        if (savedInstanceState != null) {
            ret = savedInstanceState.getString(paramName);
        } else {
            if (intent != null) {
                Bundle incomming = intent.getExtras();
                if (incomming != null) {
                    ret = incomming.getString(paramName);
                }
            }
        }

        return ret;
    }


    public static boolean interceptBooleanParam(Bundle savedInstanceState,
                                                Intent intent, String paramName) {
        boolean ret = false;

        if (savedInstanceState != null) {
            ret = savedInstanceState.getBoolean(paramName, false);
        } else {
            if (intent != null) {
                Bundle incomming = intent.getExtras();
                if (incomming != null) {
                    ret = incomming.getBoolean(paramName, false);
                }
            }
        }

        return ret;
    }


    public static int interceptIntParam(Bundle savedInstanceState,
                                        Intent intent, String paramName) {
        int ret = -1;

        if (savedInstanceState != null) {
            ret = savedInstanceState.getInt(paramName, -1);
        } else {
            if (intent != null) {
                Bundle incomming = intent.getExtras();
                if (incomming != null) {
                    ret = incomming.getInt(paramName, -1);
                }
            }
        }

        return ret;
    }


    public static float interceptFloatParam(Bundle savedInstanceState,
                                            Intent intent, String paramName) {
        float ret = 0;

        if (savedInstanceState != null) {
            ret = savedInstanceState.getFloat(paramName, 0);
        } else {
            if (intent != null) {
                Bundle incomming = intent.getExtras();
                if (incomming != null) {
                    ret = incomming.getFloat(paramName, 0);
                }
            }
        }

        return ret;
    }


    public static double interceptDoubleParam(Bundle savedInstanceState,
                                              Intent intent, String paramName) {
        double ret = 0;

        if (savedInstanceState != null) {
            ret = savedInstanceState.getDouble(paramName, 0);
        } else {
            if (intent != null) {
                Bundle incomming = intent.getExtras();
                if (incomming != null) {
                    ret = incomming.getDouble(paramName, 0);
                }
            }
        }

        return ret;
    }


    public static byte interceptByteParam(Bundle savedInstanceState,
                                          Intent intent, String paramName) {
        byte ret = 0;

        if (savedInstanceState != null) {
            ret = savedInstanceState.getByte(paramName, (byte) 0);
        } else {
            if (intent != null) {
                Bundle incomming = intent.getExtras();
                if (incomming != null) {
                    ret = incomming.getByte(paramName, (byte) 0);
                }
            }
        }

        return ret;
    }


    public static char interceptCharParam(Bundle savedInstanceState,
                                          Intent intent, String paramName) {
        char ret = 0;

        if (savedInstanceState != null) {
            ret = savedInstanceState.getChar(paramName, (char) 0);
        } else {
            if (intent != null) {
                Bundle incomming = intent.getExtras();
                if (incomming != null) {
                    ret = incomming.getChar(paramName, (char) 0);
                }
            }
        }

        return ret;
    }
}