简体   繁体   English

Android AlarmManager立即启动

[英]Android AlarmManager starts immediately

In my database I have a list of date and time in this format "yyyy-mm-dd hh: mm". 在我的数据库中,我以“ yyyy-mm-dd hh:mm”的格式列出了日期和时间。 At each date and time I want to start a corresponding notification. 我想在每个日期和时间启动相应的通知。 But instead the notification starts immediately. 但是,通知会立即开始。 What is wrong? 怎么了?

private void setAlarmFromDatabase(){            
        SQLiteDatabase db = mHelper.getReadableDatabase();
        String sql = "SELECT _id, riserva_3 FROM riserva";
        Cursor c = db.rawQuery(sql, null);          
         int count = c.getCount();
            long[] data_ora = new long[count];
            for(int i=0; i<count; i++) {
                c.moveToNext();    
                String id = c.getString(0);
                data_ora[i] = c.getLong(1);     

            }

        c.close();
    db.close();

     AlarmManager[] alarmManager=new AlarmManager[24];
        for(int ii=0;ii<data_ora.length;ii++)
        {
           alarmManager[ii] = (AlarmManager) getSystemService(ALARM_SERVICE);               

           Intent i = new Intent(getBaseContext(), Notifica_scadenza.class);

          PendingIntent displayIntent=PendingIntent.getBroadcast(getBaseContext(),ii,i,0);
          alarmManager[ii].set(AlarmManager.RTC_WAKEUP,(data_ora[ii]), displayIntent);
        }
        this.finish();
        overridePendingTransition(R.anim.right_in, R.anim.left_out);
        }

} The BroadcastReceiver } BroadcastReceiver

public class Notifica_scadenza extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
    // Vibrate the mobile phone
    Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(1000);
    showNotification(context);
}
private void showNotification(Context context) {
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, Inserisci_entrate_uscite.class), 0);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.allarm_icon)
            .setContentTitle(context.getString(R.string.app_name))
            .setContentText(context.getString(R.string.titolo_scadenza));
    mBuilder.setContentIntent(contentIntent);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);

    NotificationManager mNotificationManager =
    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());

}  
}

the permission in the Manifest 清单中的许可

    <uses-permission android:name="android.permission.VIBRATE" android:required="false"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name="scad.Notifica_scadenza"></receiver>

Like CommonsWare mentioned most probably the problem is with the date conversion. 就像提到的CommonsWare一样,问题可能出在日期转换上。 Before the for loop define also a date formatter which you can use to format the received date from the database. 在for循环之前,还定义一个日期格式化程序,您可以使用该数据格式化程序来格式化从数据库接收的日期。

final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());

Now in the for loop try this: 现在在for循环中尝试以下操作:

try {
    data_ora[i] = dateFormat.parse(cursor.getString(1)).getTime();
} catch (ParseException e) {
    Log.e(TAG, "Something wrong happened while parsing the date.", e);
}

You can also log out the data in order to see if the data is correct or not (right after the above line). 您也可以注销数据以查看数据是否正确(在上一行之后)。

Log.i("YOUR_TAG", "The id is: " + id + ", and the time: " + data_ora[i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM