【问题标题】:How do I schedule weekly notifications with data from EditTexts?如何使用来自 EditText 的数据安排每周通知?
【发布时间】:2014-12-24 12:45:04
【问题描述】:

我正在开发一个非常基本的时间表应用程序作为高中的研究项目,但遇到了一些我找不到解决方案的问题。

我的主要(也是唯一的)布局包含一个 TabHost,其中包含五个选项卡(周一至周五)。

每个选项卡包含 6 行(六个不同时间)的水平线性布局,其中包含两个 EditText:一个用于主题标题,另一个用于班级编号。这意味着用户可以在不同的编辑文本中输入其个人数据。例如在星期一 8:00 安排的通知应使用来自这些 EditTexts 的数据构建。

我想做的是每周创建 30 个通知,从星期一到星期五每天 6 个。它们必须被触发的时间是预先定义的,用户将无法更改它。

Here is an image of the layout.

在 activity_main.xml 布局中复制 30 次的基本代码:

                    <LinearLayout
                       android:layout_width="wrap_content"
                       android:layout_height="0dp"
                       android:orientation="horizontal"
                       android:layout_weight="1"
                       android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/first_time"
                        android:textSize="20sp"/>

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:hint="@string/subject_hint"
                        android:layout_marginStart="10dp"
                        android:inputType="number|textCapWords"
                        android:id="@+id/mon_subj_1" />

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:hint="@string/class_hint"
                        android:layout_marginStart="10dp"
                        android:id="@+id/mon_class_1" />

                    <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/button_action_delete"
                        android:layout_marginStart="10dp"
                        android:contentDescription="@string/button_description"/>

                    </LinearLayout>

在 MainActivity.java 文件中,我只添加了设置 TabHost 所需的代码:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //creating the TAB HOST
        TabHost tabHost = (TabHost) findViewById(R.id.tabHost2);
        tabHost.setup();
        //setting up the 1st tab
        TabHost.TabSpec tabSpec = tabHost.newTabSpec("monday");
        tabSpec.setContent(R.id.monday);
        tabSpec.setIndicator("Mon");
        tabHost.addTab(tabSpec);
        //setting up the 2nd tab
        tabSpec = tabHost.newTabSpec("tuesday");
        tabSpec.setContent(R.id.tuesday);
        tabSpec.setIndicator("Tue");
        tabHost.addTab(tabSpec);
        //setting up the 3rd tab
        tabSpec = tabHost.newTabSpec("wednesday");
        tabSpec.setContent(R.id.wednesday);
        tabSpec.setIndicator("Wed");
        tabHost.addTab(tabSpec);
        //setting up the 4th tab
        tabSpec = tabHost.newTabSpec("thursday");
        tabSpec.setContent(R.id.thursday);
        tabSpec.setIndicator("Thu");
        tabHost.addTab(tabSpec);
        //setting up the 5th tab
        tabSpec = tabHost.newTabSpec("friday");
        tabSpec.setContent(R.id.friday);
        tabSpec.setIndicator("Fri");
        tabHost.addTab(tabSpec);
        //here we have finished creating the TAB HOST

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

标签: java android


【解决方案1】:

AlarmManager 允许您安排您的应用程序在未来某个时间点运行。当警报响起时,系统会广播已为其注册的Intent,如果目标应用程序尚未运行,则会自动启动它。 *

【讨论】:

    猜你喜欢
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 2015-10-18
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    相关资源
    最近更新 更多