这是我的警报管理器代码。我想根据数据库的结果注册系统警报服务。
public void onReceive(Context context, Intent intent) {
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
SQLiteDatabase database = new TaskDatabaseManager(context)
.getWritableDatabase();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd",
Locale.US);
SimpleDateFormat dateFormat2 = new SimpleDateFormat("HH:mm", Locale.US);
String today = dateFormat.format(new Date());
Cursor c = database
.rawQuery("Select " + TaskDatabaseManager.TASK_TIMES2
+ " from " + TaskDatabaseManager.DATABASE_TABLE
+ " where " + TaskDatabaseManager.TASK_DATE + "= ?",
new String[] { today });
Intent intent1 = new Intent(context, NotificationService.class);
c.moveToFirst();
if (c.getCount() != 0) {
do {
try {
long mili = dateFormat2.parse(c.getString(0)).getTime();
PendingIntent p = PendingIntent.getService(context, 0,
intent1, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, mili, p);
Toast.makeText(context, "Next Next",
Toast.LENGTH_LONG).show();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while (c.moveToNext());
}
}
下面是我的服务类。现在,为了测试这部分代码,我正在使用 Toast。
public class NotificationService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "Worked", Toast.LENGTH_LONG).show();
return START_STICKY;
基本上,我想启动一项服务,该服务将在存储在数据库中的特定时间显示特定通知。时间由用户输入,并在此基础上创建一个通知。我是安卓新手。