0

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

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

这是我的主要活动:

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

}

这是我的通知处理程序类:

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


    }
}

谢谢。

4

1 回答 1

0

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

从 onClick() 调用 setAlarm()。

于 2013-11-13T15:49:38.650 回答