3

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

我已经使用日历类设置了年份、日期时间、月份、秒,但无法获得通知。

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

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();
    }

}
4

1 回答 1

1

改变

Calendar.setField(Calendar.HOUR, xx)

Calendar.setField(Calendar.HOUR_OF_DAY, xx)

于 2012-08-15T07:32:39.550 回答