如何在android中的应用程序启动时设置不同的消息?

时间:2012-03-21 05:29:42

标签: android message startup

我正在为学生创建一个应用程序。每当我需要设置不同的消息 用户打开应用程序。我不明白如何使用此方法或使用哪种方法。
我搜索了很多文章,但我没有找到任何东西 所以请提供一些参考或代码。

4 个答案:

答案 0 :(得分:2)

您可以使用sqlite,file或sharedpreferences之类的任何存储来存储您的消息,并在应用程序打开时随机检索消息。

答案 1 :(得分:1)

你应该阅读关于android的基础知识,这将指导你如何做到这一点。你不会向用户喷出垃圾,会有一种模式。找到模式后,将该逻辑转换为java。

答案 2 :(得分:1)

将邮件保存在永久存储中。在android中,您可以使用SharedPreference http://developer.android.com/reference/android/content/SharedPreferences.html或Sqlite数据库http://developer.android.com/reference/android/database/sqlite/package-summary.html,具体取决于您的具体需求。将消息存储在其中任何一个中,每次都读回不同的消息。 在Activity中的某个位置将一些msg存储在SharedPreference中:

SharedPreferences pref =    getPreferences(Context.MODE_PRIVATE);
        Editor ed =pref.edit();
        ed.putString("0","msg0");
        ed.putString("1","msg1");
        ed.putString("2","msg2");
        ed.putString("3","msg3");
        ed.commit();

然后在onCreate()中,检索随机sg和diplay给用户:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences pref =    getPreferences(Context.MODE_PRIVATE);
        Random r = new Random();

        String msg = pref.getString(r.nextInt(4)+"", "none");
        Toast.makeText(this, msg, Toast.LENGTH_LONG ).show();
}

答案 3 :(得分:0)

使用AlertDialog

以下代码示例取自: http://www.mkyong.com/android/android-alert-dialog-example/

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

// set title
alertDialogBuilder.setTitle("Your Title");

// set dialog message
alertDialogBuilder.setMessage("Click yes to exit!").setCancelable(false)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // if this button is clicked, close
                // current activity
                MainActivity.this.finish();
            }
        }).setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // if this button is clicked, just close
                // the dialog box and do nothing
                dialog.cancel();
            }
        });

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();