Android 一段时间后如何显示通知?

Android 一段时间后如何显示通知?,android,Android,我需要在点击按钮时显示通知,但不是在那个时候,而是在一段时间后,比如几天。我已经为此编写了代码,但并没有显示输出 请检查并告诉我哪里错了,我应该添加什么使其工作 这是我的主要活动: public class MainActivity extends Activity { private ImageButton Button1; private int year; private int month; private int date; private i

我需要在点击按钮时显示通知,但不是在那个时候,而是在一段时间后,比如几天。我已经为此编写了代码,但并没有显示输出

请检查并告诉我哪里错了,我应该添加什么使其工作

这是我的主要活动:

public class MainActivity extends Activity {

    private ImageButton Button1;
    private int year;
    private int month;
    private int date;
    private int hour;
    private int minute;
    private int second;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button1 = (ImageButton) findViewById(R.id.Button1);
        Button1.setTag("ON");
        //Button1.setSelected(selected);
        Button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

        if (v.getTag().toString().equals("ON")) {
                    v.setTag("OFF");
                     v.setBackgroundResource(R.drawable.star_off);
                    // NotificationHandler notification = new NotificationHandler(MainActivity.this);

                    //notification.sendNotification("New Notification Received");
                    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    //Notification notification = new Notification(R.drawable.icon, "A New Message!", System.currentTimeMillis());
                }else {
                    v.setTag("ON");
                     v.setBackgroundResource(R.drawable.star_on);
                }


            }
        });
    }



    public void startAlarm() {
        @SuppressWarnings("static-access")
        AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
        Calendar calendar =  Calendar.getInstance();
        calendar.set(2013, 11,13, 8,  30,  1);
        long when = calendar.getTimeInMillis();         // notification time
                Intent intent = new Intent(this, ReminderService.class);
                PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
                alarmManager.set(AlarmManager.RTC, when, pendingIntent);
            }



    public class ReminderService extends IntentService {
        private static final int NOTIF_ID = 1;

        public ReminderService(){
            super("ReminderService");
        }

        @SuppressWarnings({ "deprecation", "static-access" })
        @Override
          protected void onHandleIntent(Intent intent) {
            NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            long when = System.currentTimeMillis();         // notification time

            Notification notification = new Notification(R.drawable.ic_launcher, "reminder", when);
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.flags |= notification.FLAG_AUTO_CANCEL;
            Intent notificationIntent = new Intent(this, MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);
            notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);
            nm.notify(NOTIF_ID, notification);
        }

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
这是我的notificationhandler类:

public class NotificationHandler {


    Context context = null;
    public int contentinfo = 1;


    public NotificationHandler(Context context){

        this.context = context;
    }

    //Send Notification start

    public void sendNotification(String message) {

        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE); 

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);

        alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


            contentinfo++;
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("New Message")
            .setContentText("messgae")
            .setContentInfo(""+contentinfo)
            .setSound(alarmSound)
            .setAutoCancel(true)
            .setStyle(new NotificationCompat.InboxStyle());
            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(context, MainActivity.class);


            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(MainActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(
                            0,
                            PendingIntent.FLAG_UPDATE_CURRENT
                            );
            mBuilder.setContentIntent(resultPendingIntent);

            // mId allows you to update the notification later on.
            int mId = 1;
            notificationManager.notify(mId, mBuilder.build());



        //Send Notification end


    }
}

谢谢。

我没有填写完整的通知代码。首先,代码中没有调用startAlarm()函数。


从onClick()调用setAlarm()

这些事件在未来有多远?您的应用程序此时是否仍在运行?我也想知道,你为什么要做到这一点?这听起来像讨厌的广告,再次打开你的应用程序。可能是在几天之后。我的应用程序正在运行。抱歉,但这是我的要求。考虑使用<代码>日历>()(<)>代码>,而不是<代码>日历>()(< /代码>)。这很可能是你的问题。硬编码日期是完全错误的。