我需要用户提供一个时间,当这个时间到达时,通知会被解雇,我该怎么办呢?

时间:2011-12-03 11:59:55

标签: java android time notifications

我需要用户提供一个时间,当这个时间到来时,会发出通知。 我怎样才能做到这一点?

public void setReminder(Long taskId, Calendar when) {

    Intent i = new Intent(mContext, OnAlarmReceiver.class);
    i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId); 

    PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT); 

    mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
}

4 个答案:

答案 0 :(得分:1)

步骤1:您必须为此创建服务,我的服务是MyAlarmService.class,如下所示

    public class MyAlarmService extends Service {

    @Override
    public void onCreate() {
    // Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
     //Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
     return null;
    }

    @Override
    public void onDestroy() {
     super.onDestroy();
     //Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        try
        {

            Context context = getApplicationContext();

            // Displaying Notification
              NotificationManager manger = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
               Notification notification = new Notification(R.drawable.icon, "Notification", System.currentTimeMillis());

    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), Target.class), 0);

                notification.setLatestEventInfo(getApplicationContext(), "Notification", "Notification subject", contentIntent);
                notification.flags = Notification.FLAG_INSISTENT;

            int NOTIFICATION_ID=(int)System.currentTimeMillis();
                manger.notify(NOTIFICATION_ID, notification);



        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        // TODO Auto-generated method stub
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onStart(Intent intent, int startId) 
    {
       super.onStart(intent, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
      return super.onUnbind(intent);
    }

    }

步骤2:您必须创建一个待处理的Intent,如下所示

    Intent myIntent = new Intent(SynchronizeData.this, MyAlarmService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this.getApplicationContext(), 0, myIntent, 0);


    Step 3 : Create set Alarm 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP,triggerTime, pendingIntent);

    // trigger time is your time interval in milliseconds 

注意:不要忘记在AndroidManifest.xml中声明服务

答案 1 :(得分:1)

public void setAlarm(long startTime) 
     {
        // We have to register to AlarmManager
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        if(startTime>=100000)
        {
         startTime /= 1000;
         int secs = (int)(startTime);
         calendar.add(Calendar.SECOND, secs);
        }
        else
        {
            int secss = (int)startTime;
             calendar.add(Calendar.MILLISECOND, secss);
        }
        // We set a one shot alarm
        long triggerTime = calendar.getTimeInMillis();

        //Intent myIntent = new Intent(SettingScreen.this, blue.com.api.tab.BroadCast.MyAlarmService.class);
     // pendingIntent = PendingIntent.getService(SettingScreen.this, 0, myIntent, 0);

        alarmManager.set(AlarmManager.RTC_WAKEUP,triggerTime, pendingIntent);
     }

答案 2 :(得分:1)

// Calculating the difference 
                       long currentmillis = System.currentTimeMillis();

                       final Calendar sync= Calendar.getInstance();
                       sync.set(sync.get(Calendar.YEAR), sync.get(Calendar.MONTH), sync.get(Calendar.DAY_OF_MONTH), mHour, mMinute,0);
                       long millissync = sync.getTimeInMillis();                

                       long alertmillis = (millissync - currentmillis);
                       if(alertmillis>=1000)
                         setAlarm(alertmillis);

答案 3 :(得分:0)

这并不像听起来那么容易:

使用Androids AlarmManager设置警报。

使用BroadcastReciever接收您的闹钟

在Broadcast the NotificationManager中触发