0

这是我的警报管理器代码。我想根据数据库的结果注册系统警报服务。

            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;

基本上,我想启动一项服务,该服务将在存储在数据库中的特定时间显示特定通知。时间由用户输入,并在此基础上创建一个通知。我是安卓新手。

4

2 回答 2

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);
于 2014-03-11T08:19:02.753 回答
0

在你的清单中你写过

 <service android:enabled="true" android:name=".NotificationService " />

检查 c.getCount() 是否为非零。

于 2013-09-02T13:16:22.593 回答