无法获取在android中设置的特定日期和时间的通知

时间:2012-08-15 07:10:16

标签: android calendar notifications

无法使用日历类获取特定日期设置的通知。

我使用日历类设置了年份,日期时间,月份,秒,无法获取通知。

任何帮助将不胜感激,下面是代码片段。

public class CalActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Intent chartNotifier = new Intent(CalActivity.this,
                CRT.class);


        PendingIntent pendingIntent = PendingIntent
                .getBroadcast(
                        CalActivity.this,
                        234324243,
                        chartNotifier,
                        PendingIntent.FLAG_UPDATE_CURRENT);
        Calendar calendar = Calendar.getInstance();
        //calendar.set(year, month, day, hour, min, 0);
        //calendar.set(2012, 8, 15, 12, 18,0);
        calendar.set(Calendar.YEAR,2012);
        calendar.set(Calendar.MONTH, 8-1);
        calendar.set(Calendar.DAY_OF_MONTH,15);
        calendar.set(Calendar.HOUR, 12);
        calendar.set(Calendar.MINUTE,32);
        calendar.set(Calendar.SECOND, 15);

        long when =calendar.getTimeInMillis();

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP,when,
                pendingIntent);

    }
}


Receiver Class

public class CRT extends BroadcastReceiver {

    String trainNo, journeyDate;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        System.out.println("Has arrived inside Receiver");
        // Call Service
        Intent service = new Intent(context, ChartNotifierService.class);

        context.startService(service);

    }

}

Service class is


package com.cal;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;


public class ChartNotifierService extends Service {

    String trainNo;
    String journeyDate;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);


        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager notificationManager = (NotificationManager) ChartNotifierService.this
                .getSystemService(ns);

        long when = System.currentTimeMillis();
        Notification notification = new Notification(R.drawable.ic_launcher, "Time",
                when);

        notification.flags |= Notification.FLAG_INSISTENT
                | Notification.FLAG_AUTO_CANCEL;


        Intent enterPnr = new Intent(ChartNotifierService.this, CalActivity.class);
        enterPnr.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent enterIntent = PendingIntent.getActivity(
                ChartNotifierService.this, 0, enterPnr, 0);
        notification.setLatestEventInfo(ChartNotifierService.this, "Date",
                "time", enterIntent);

        notificationManager.notify(0, notification);

    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }

}

1 个答案:

答案 0 :(得分:1)

更改

Calendar.setField(Calendar.HOUR, xx)

Calendar.setField(Calendar.HOUR_OF_DAY, xx)