Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;

import android.content.Context;
import android.content.res.Resources;

public class Main {

    public static String getChannelNum(Context context) {
        Resources resources = context.getResources();
        try {
            Class<?> className = Class.forName(
                    (new StringBuilder(String.valueOf(context.getPackageName()))).append(".R$raw").toString());
            Field field = className.getField("parent");
            Integer result = (Integer) field.get(className);
            InputStream num = resources.openRawResource(result);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int i = 1;
            try {
                while ((i = num.read()) != -1) {
                    baos.write(i);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                resources = null;
                if (num != null) {
                    try {
                        num.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return new String(baos.toByteArray());
        } catch (Exception e) {
        }
        return "0";
    }
}