generate Intent Filter - Android Intent

Android examples for Intent:Intent Filter

Description

generate Intent Filter

Demo Code


//package com.java2s;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.IntentFilter;

public class Main {
    private static final HashMap<Class<?>[], IntentFilter> intentFilterCache = new HashMap<Class<?>[], IntentFilter>();

    public final static IntentFilter generateIntentFilter(Class<?>[] cls) {
        if (intentFilterCache.containsKey(cls))
            return intentFilterCache.get(cls);
        IntentFilter intentFilter = new IntentFilter();
        for (Class<?> c : cls)
            for (String a : generateIntentFilterList(c))
                intentFilter.addAction(a);
        intentFilterCache.put(cls, intentFilter);
        return intentFilter;
    }//from   ww w .j a v a 2s .  c  o m

    public final static IntentFilter generateIntentFilter(Class<?> cls) {
        return generateIntentFilter(new Class<?>[] { cls });
    }

    private final static List<String> generateIntentFilterList(Class<?> cls) {
        ArrayList<String> list = new ArrayList<String>();
        for (Field field : cls.getDeclaredFields())
            if (field.getType().equals(String.class)
                    && Modifier.isStatic(field.getModifiers())
                    && field.getName().startsWith("ACTION_"))
                try {
                    list.add((String) field.get(null));
                } catch (Exception e) {
                }
        return list;
    }
}

Related Tutorials