这是我的警报管理器代码。我想根据数据库的结果注册系统警报服务。
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;
基本上,我想启动一项服务,该服务将在存储在数据库中的特定时间显示特定通知。用户输入时间,并根据该时间,它将在此时创建通知。我是Android的新手。
答案 0 :(得分:1)
正确的方法是
Intent otherIntent = new Intent();
otherIntent.setClass(this, YourService.class);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent intent = PendingIntent.getService(getApplicationContext(),
(int) System.currentTimeMillis(), otherIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarm.set(AlarmManager.RTC_WAKEUP, yourTimeInMillis(), intent);
答案 1 :(得分:0)
是否写过
<service android:enabled="true" android:name=".NotificationService " />
检查c.getCount()是否非零。