用同样的方法在整个应用程序的Andr​​oid所有选项菜单 [英] Use same method for all option menu in whole Application Android

查看:173
本文介绍了用同样的方法在整个应用程序的Andr​​oid所有选项菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有10个至12个活动,所有活动帮助菜单选项菜单
我有以下code创建,并显示在其中单击帮助成功。

I have 10 to 12 Activity, All Activity has Help Menu as an Option Menu. I am succeed with following code to create it and showing help on click of them.

    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(cacheDir, "HELP.pdf")),"application/pdf");

    context.startActivity(intent);

但我想为了减少这种code的所有活动,并为我创建一个类和做一个方法,但我仍希望减少code。

But I want to Reduce this code for all Activity, and for that i have created one class and make one method but still i want to reduce code.

我已搜查,发现的onClick 属性可在 OptionMenu ,但我没有得到如何使用吧。

I have searched and found that onClick attribute is available in OptionMenu but I didn't get how to use it.

请帮助..

推荐答案

我创建了以下班中openFile:

I have created following Class for openFile:

public class OpenHelpFile {

    File cacheDir;
    Context context;

    /* Constructor */
    public OpenHelpFile(Context context) {
        this.context = context;

        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "OOPS");
        else
            cacheDir = context.getCacheDir();

        if (!cacheDir.exists())
            cacheDir.mkdirs();

        try {
            File helpFile = new File(cacheDir, "OOPS.pdf");

            if (!helpFile.exists()) {
                InputStream in = context.getAssets().open("OOPS.pdf");
                OutputStream out = new FileOutputStream(helpFile);

                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();

                Log.d(TAG, "File Copied...");
            }
            Log.d(TAG, "File exist...");

        } catch (FileNotFoundException ex) {
            Log.d(TAG, ex.getMessage() + " in the specified directory.");
            System.exit(0);
        } catch (IOException e) {
            Log.d(TAG, e.getMessage());
        }
    }

    public void openHelpFile() {

        /* OPEN PDF File in Viewer */
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(cacheDir, "OOPS.pdf")), "application/pdf");

        context.startActivity(intent);
    }
}

和我有每个调用它 OptionMenu 是这样的:

and I have call it from every OptionMenu like this:

new OpenHelpFile(context).openHelpFile();

这篇关于用同样的方法在整个应用程序的Andr​​oid所有选项菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆