阅读应用程序的Widget txt文件 [英] Read txt file in app Widget

查看:155
本文介绍了阅读应用程序的Widget txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要阅读应用的Widget TXT文件。

I need read txt file in app Widget.

我建立TXT在外部设定的活动名称为NASTAVENI

I create txt in external set activity with name "NASTAVENI":

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.set);
    editor=(EditText)findViewById(R.id.editor);
    Button btn=(Button)findViewById(R.id.close);    
    btn.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            phoneNo = editor.getText().toString(); 
            finish();
        }
    });
}

public void onResume() {
    super.onResume();

    try {
        InputStream in=openFileInput("notes.txt");

        if (in!=null) {
            InputStreamReader tmp=new InputStreamReader(in);
            BufferedReader reader=new BufferedReader(tmp);
            String str;
            StringBuilder buf=new StringBuilder();

            while ((str = reader.readLine()) != null) {
                buf.append(str+"\n");
            }

            in.close();
            editor.setText(buf.toString());
        }
    }
    catch (java.io.FileNotFoundException e) {

    }
    catch (Throwable t) {
        Toast
            .makeText(this, "Exception: "+t.toString(), 2000)
            .show();
    }
}

/*BACK**************************************************************************************************************************************/
public void onPause() {
    super.onPause();

    try {
        OutputStreamWriter out=
                new OutputStreamWriter(openFileOutput("notes.txt", 0));

        out.write(editor.getText().toString());
        out.close();        
    }
    catch (Throwable t) {
        Toast
            .makeText(this, "Exception: "+t.toString(), 2000)
            .show();
    }
}

}

有关应用的Widget主要活动是与名为小工具。我需要在这个活动窗口小部件读外部活动NASTAVENI的TXT文件。活动的Widget和NASTAVENI都在同一个包。

Main activity for app Widget is with name "Widget". I need read txt file of external activity "NASTAVENI" in this activity "Widget". Activity Widget and NASTAVENI are in the same package.

这code是不好的(错误显示java.lang.NullPointerException)

This code is bad (error java.lang.NullPointerException)

   try {
                    InputStream in=openFileInput("notes.txt");


                        InputStreamReader tmp=new InputStreamReader(in);
                        BufferedReader reader=new BufferedReader(tmp);
                        String str;
                        StringBuilder buf=new StringBuilder();

                        while ((str = reader.readLine()) != null) {
                            buf.append(str+"\n");
                        }

                        in.close();
                        editor.setText(buf.toString());
                        phoneNo = "77"; 

                }
                catch (java.io.FileNotFoundException e) {
                    Toast toast=Toast.makeText(context, "Error FFE!", 1000);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
                catch (Throwable t) {
                    Toast toast=Toast.makeText(context, "Error T!" +t.toString(), 1000);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
}

对不起,我的英语

Sorry for my English

推荐答案

我,如果你想只是一个小部件,从一个txt读取或者你也想活动我不知道。
对于阅读txt文件,我用它来创建一个类,读取该文件,并给我回我需要什么,在这样的功能:

I am not sure if you want just a widget that reads from a txt or you want also "an activity". For the reading the txt file, I use to create a class that reads the file and give me back what I need, in a function like this:

  ShowLine(){
    try {
                InputStream is = mContext.getAssets().open(monthFile);
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                // Skips lines
                Log.w("day", String.valueOf(monthFile));
                for (int i = 0; i< DayMonth-1; i++) {
                    reader.readLine();
                }
                Line = reader.readLine(); // read the line of the day

                Name= Line.substring(Line.indexOf("*") + 1,Line.indexOf("-"));// I read between + and (my data is like: DAY * NAME -)
                dia= ""+DayMonth+" de "+ monthNames[Month]+"" ;
                Log.w("Name", String.valueOf(Name)); //to check that it works

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

有关小部件,你需要创建一个类,是一个小部件有:

For the widget , you need to create a class that is a Widget with :

public class Widget extends AppWidgetProvider {
String dia = "1 Feb.";
String Name= "Pavel";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){

        mContext = context;
        Resources res = mContext.getResources();

        ComponentName thisWidget = new ComponentName(context,Widget.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        for(int widgetIds: allWidgetIds){


            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
            //Log.w("Widget test", String.valueOf(number));

            try{
                ShowLine();
                Log.w("Line 1", String.valueOf(Name));

            }catch (Exception e) {
                Name= "error";
            }

我不知道我的code完美的作品,也许你已经完成了一点,因为我没贴全功能。但是,如果你有疑问,这是一个很好的教程: vogella.com
我希望它可以帮助你:)

I am not sure my code works perfect, probably you have to complete it a bit, because I didn't paste the whole functions. But If you have doubts , this is a good tutorial: vogella.com I hope it helps you :)

这篇关于阅读应用程序的Widget txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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