以下是一个完整的示例代码,实现了记录每天学习内容并推送未完成任务的功能:

MainActivity.java:

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private Button saveButton;
    private ListView listView;
    private List<String> studyList;
    private StudyListAdapter adapter;

    private static final String PREFS_NAME = "StudyPrefs";
    private static final String KEY_STUDY_LIST = "studyList";

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

        editText = findViewById(R.id.edit_text);
        saveButton = findViewById(R.id.save_button);
        listView = findViewById(R.id.list_view);

        studyList = new ArrayList<>();
        adapter = new StudyListAdapter(this, studyList);
        listView.setAdapter(adapter);

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String studyContent = editText.getText().toString().trim();
                if (!studyContent.isEmpty()) {
                    studyList.add(studyContent);
                    adapter.notifyDataSetChanged();
                    editText.setText("");

                    saveStudyList();
                } else {
                    Toast.makeText(MainActivity.this, "请输入学习内容", Toast.LENGTH_SHORT).show();
                }
            }
        });

        loadStudyList();
        registerAlarmReceiver();
    }

    private void saveStudyList() {
        SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        StringBuilder stringBuilder = new StringBuilder();
        for (String studyContent : studyList) {
            stringBuilder.append(studyContent).append(",");
        }
        editor.putString(KEY_STUDY_LIST, stringBuilder.toString());
        editor.apply();
    }

    private void loadStudyList() {
        SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        String studyContentString = sharedPreferences.getString(KEY_STUDY_LIST, "");
        String[] studyContents = studyContentString.split(",");
        for (String studyContent : studyContents) {
            if (!studyContent.isEmpty()) {
                studyList.add(studyContent);
            }
        }
        adapter.notifyDataSetChanged();
    }

    private void registerAlarmReceiver() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 8); // 设置每天的推送时间,这里设置为早上8点
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        Intent intent = new Intent(this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }
}

StudyListAdapter.java:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.List;

public class StudyListAdapter extends ArrayAdapter<String> {

    private Context context;
    private List<String> studyList;

    public StudyListAdapter(Context context, List<String> studyList) {
        super(context, 0, studyList);
        this.context = context;
        this.studyList = studyList;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.list_item_study, parent, false);
        }

        String studyContent = studyList.get(position);

        TextView studyTextView = view.findViewById(R.id.study_text_view);
        studyTextView.setText(studyContent);

        return view;
    }
}

AlarmReceiver.java:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class AlarmReceiver extends BroadcastReceiver {

    private static final String PREFS_NAME = "StudyPrefs";
    private static final String KEY_STUDY_LIST = "studyList";

    @Override
    public void onReceive(Context context, Intent intent) {
        List<String> studyList = loadStudyList(context);
        List<String> unfinishedTasks = getUnfinishedTasks(studyList);

        if (!unfinishedTasks.isEmpty()) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("未完成的任务:\n");
            for (String task : unfinishedTasks) {
                stringBuilder.append("- ").append(task).append("\n");
            }
            Toast.makeText(context, stringBuilder.toString(), Toast.LENGTH_LONG).show();
        }
    }

    private List<String> loadStudyList(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        String studyContentString = sharedPreferences.getString(KEY_STUDY_LIST, "");
        String[] studyContents = studyContentString.split(",");
        List<String> studyList = new ArrayList<>();
        for (String studyContent : studyContents) {
            if (!studyContent.isEmpty()) {
                studyList.add(studyContent);
            }
        }
        return studyList;
    }

    private List<String> getUnfinishedTasks(List<String> studyList) {
        List<String> unfinishedTasks = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        if (!studyList.contains("政治") && (currentDayOfWeek == Calendar.MONDAY || currentDayOfWeek == Calendar.WEDNESDAY || currentDayOfWeek == Calendar.FRIDAY)) {
            unfinishedTasks.add("政治");
        }
        if (!studyList.contains("英语") && (currentDayOfWeek == Calendar.TUESDAY || currentDayOfWeek == Calendar.THURSDAY || currentDayOfWeek == Calendar.SATURDAY)) {
            unfinishedTasks.add("英语");
        }
        if (!studyList.contains("数学") && (currentDayOfWeek == Calendar.WEDNESDAY || currentDayOfWeek == Calendar.FRIDAY || currentDayOfWeek == Calendar.SUNDAY)) {
            unfinishedTasks.add("数学");
        }
        if (!studyList.contains("专业课") && (currentDayOfWeek == Calendar.THURSDAY || currentDayOfWeek == Calendar.SATURDAY || currentDayOfWeek == Calendar.SUNDAY)) {
            unfinishedTasks.add("专业课");
        }

        return unfinishedTasks;
    }
}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingTop="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入学习内容" />

    <Button
        android:id="@+id/save_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_text"
        android:layout_alignParentEnd="true"
        android:text="保存" />

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/save_button"
        android:layout_marginTop="16dp" />

</RelativeLayout>

list_item_study.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/study_text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp" />

请注意,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。另外,记得在 AndroidManifest.xml 文件中注册 AlarmReceiver。

Android 学习任务管理:每日推送未完成内容

原文地址: https://www.cveoy.top/t/topic/QkQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录