Android 学习内容记录与提醒应用开发教程
好的,以下是一个简单的示例代码,实现了记录每天学习内容并推送未完成任务的功能:
- 创建一个新的 Android 项目,并在布局文件中添加一个 EditText 用于输入学习内容,一个 Button 用于保存学习内容,以及一个 ListView 用于显示已保存的学习内容。
activity_main.xml:
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
android:layout_width='match_parent'
android:layout_height='match_parent'
android:orientation='vertical'>
<EditText
android:id='@+id/editText'
android:layout_width='match_parent'
android:layout_height='wrap_content'
android:hint='输入学习内容' />
<Button
android:id='@+id/saveButton'
android:layout_width='wrap_content'
android:layout_height='wrap_content'
android:text='保存' />
<ListView
android:id='@+id/listView'
android:layout_width='match_parent'
android:layout_height='match_parent' />
</LinearLayout>
- 在 MainActivity 中,定义一个 ArrayList
用于存储学习内容。在 onCreate 方法中,初始化 ListView 并设置适配器,将 ArrayList 与 ListView 进行绑定。
MainActivity.java:
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button saveButton;
private ListView listView;
private ArrayAdapter<String> adapter;
private ArrayList<String> studyContentList;
private static final String PREFS_NAME = 'StudyContentPrefs';
private static final String KEY_STUDY_CONTENT = 'studyContent';
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
saveButton = findViewById(R.id.saveButton);
listView = findViewById(R.id.listView);
studyContentList = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, studyContentList);
listView.setAdapter(adapter);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String studyContent = editText.getText().toString();
studyContentList.add(studyContent);
adapter.notifyDataSetChanged();
saveStudyContent();
}
});
loadStudyContent();
}
private void saveStudyContent() {
SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Set<String> studyContentSet = new HashSet<>(studyContentList);
editor.putStringSet(KEY_STUDY_CONTENT, studyContentSet);
editor.apply();
}
private void loadStudyContent() {
SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
Set<String> studyContentSet = sharedPreferences.getStringSet(KEY_STUDY_CONTENT, null);
if (studyContentSet != null) {
studyContentList.addAll(studyContentSet);
adapter.notifyDataSetChanged();
}
}
}
- 在每天的特定时间,使用 AlarmManager 来触发一个广播接收器。在广播接收器中,获取 SharedPreferences 中保存的学习内容,并将未完成的任务推送给用户。
StudyContentReceiver.java:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;
import java.util.HashSet;
import java.util.Set;
public class StudyContentReceiver extends BroadcastReceiver {
private static final String PREFS_NAME = 'StudyContentPrefs';
private static final String KEY_STUDY_CONTENT = 'studyContent';
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
Set<String> studyContentSet = sharedPreferences.getStringSet(KEY_STUDY_CONTENT, null);
if (studyContentSet != null) {
for (String studyContent : studyContentSet) {
Toast.makeText(context, '未完成的任务:' + studyContent, Toast.LENGTH_SHORT).show();
}
}
}
}
- 在 AndroidManifest.xml 文件中注册广播接收器和设置 AlarmManager。
AndroidManifest.xml:
<manifest xmlns:android='http://schemas.android.com/apk/res/android'
package='com.example.studyapp'>
<uses-permission android:name='android.permission.SET_ALARM' />
<application
...>
<receiver
android:name='.StudyContentReceiver'
android:enabled='true'
android:exported='true' />
<activity
android:name='.MainActivity'
...>
...
</activity>
</application>
</manifest>
在 MainActivity 的 onCreate 方法中,添加以下代码来设置 AlarmManager,每天的特定时间触发广播接收器:
// 设置每天的特定时间(例如晚上10点)触发广播接收器
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 22);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(this, StudyContentReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
这样,你就可以实现一个记录每天学习内容并推送未完成任务的 Android 应用了。请注意,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。
原文地址: https://www.cveoy.top/t/topic/Qjz 著作权归作者所有。请勿转载和采集!