在onCreat()之外执行方法-无需输入活动即可工作

时间:2018-07-08 20:15:32

标签: android

我已经对TimeTable应用进行了编程,以帮助我了解讲座并在讲座时间到来时提醒我,问题是,除非打开以下窗口,否则获取下一个lucture(getNextLecture();)的功能将不起作用。活动,因为该函数存在于活动的OnCreat()方法中。 这是我的代码(不是完整的代码!):

主要活动(AlarmMe):

public class AlarmMe extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    setContentView(R.layout.activity_main);
    Log.i(AlarmMe.class.getName(), "AlarmMe opened!");
    timetableDao = new TimetableDao(this);
    **getNextLecture();**

    Log.i(TAG, "AlarmMe.onCreate()");

    mAlarmList = (ListView)findViewById(R.id.alarm_list);

    mAlarmListAdapter = new AlarmListAdapter(this);
    mAlarmList.setAdapter(mAlarmListAdapter);
    registerForContextMenu(mAlarmList);


    mAlarm = new Alarm(this);
    mAlarm.fromIntent(getIntent());

    mDateTime = new DateTime(this);

    mTitle="Time is up !!";
    mAlarm.setTitle(mTitle);
}



private void getNextLecture() {
    int day = MyTime.CURRENT_DAY();
    TextView tv = (TextView) findViewById(R.id.textView_next_class);
    if (day != Calendar.getInstance().get(Calendar.DAY_OF_WEEK)) {
        /* It's weekend! */
        tv.setText("No classes in the weekend!");
    } else {
    List<TimetableEntry> entries = timetableDao.getMyTimetableForDay(
                MyTime.CURRENT_SEMESTER(), day,
            MyTime.CURRENT_TIME());

    if (entries.size() == 0) {
        tv.setText("No more classes today!");
    } else {
        TimetableEntry nextClass = entries.get(0);

        String courseAndLocation = "<b>" + nextClass.getCourse().getName()
                + "</b>";
        courseAndLocation += "<br/>&nbsp;&nbsp;&nbsp;Building: ";
        Building building = nextClass.getBuilding();
        if (building != null) {
            courseAndLocation += "<a href=\"" + building.getMap() + "\">";
            courseAndLocation += building.getDescription();
            courseAndLocation += "</a>";
        } else {
            courseAndLocation += nextClass.getBuildingName();
        }

        Room room = nextClass.getRoom();
        courseAndLocation += "<br/>&nbsp;&nbsp;&nbsp;Room: ";
        if (room != null) {
            courseAndLocation += room.getDescription();
        } else {
            courseAndLocation += nextClass.getRoomName();
        }

        if (!"".equals(nextClass.getComment())) {
            courseAndLocation += "<br/>&nbsp;&nbsp;&nbsp;"
                    + nextClass.getComment();
        }
        tv.setText(Html.fromHtml(courseAndLocation));
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        //هنا عدل

        MyTime timenow = nextClass.getStart();
        a=timenow.toString().split(":");
        mYear = Calendar.getInstance().get(Calendar.YEAR);
        mMonth = Calendar.getInstance().get(Calendar.MONTH);
        mDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        mHour = Integer.parseInt(a[0]);
        mMinute =Integer.parseInt(a[1])-5;
        mCalendar = new GregorianCalendar(mYear, mMonth, mDay, mHour, mMinute);
        mAlarm.setDate(mCalendar.getTimeInMillis());
        Toast.makeText(getBaseContext(), "Alarm is set successfully",Toast.LENGTH_SHORT).show();
        mAlarmListAdapter.add(mAlarm);
        }
    }

0 个答案:

没有答案