Android警报通知

时间:2011-05-27 07:35:18

标签: android notifications alarm

我正在尝试在我的移动应用中实现闹钟功能。以下是我的用例

  1. 用户可以在设置提醒后从活动“A”设置提醒,用户将在设定时间前10分钟收到通知。现在,用户可以从其他活动中删除提醒,假设“B”

  2. 除此之外,我还希望用户可以从通知窗口重新安排他/她的提醒设置。

  3. 以下是我为用例1编写的代码

    private void setAlarm(){
        Intent intent = new Intent(A.this, RepeatAlarm.class);
    
        mAlarmSender = PendingIntent.getService(A.this,
            0, new Intent(A.this, RepeatAlarm.class), 0);
    
        // We want the alarm to go off 30 seconds from now.
            long firstTime = SystemClock.elapsedRealtime();
    
            // Schedule the alarm!
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            firstTime, 30*1000, mAlarmSender);
    
            // Tell the user about what we did.
            Toast.makeText(A.this, R.string.repeating_scheduled,
                    Toast.LENGTH_LONG).show();
    
    }
    
    private void stopAlarm(){
    
        // And cancel the alarm.
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.cancel(mAlarmSender);
    
            // Tell the user about what we did.
            Toast.makeText(B.this, R.string.repeating_unscheduled,
                    Toast.LENGTH_LONG).show();
    
    }
    

    ==============

    public class RepeatAlarm extends Service {
        NotificationManager mNM;
        @Override
        public void onCreate() {
            mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    
    
            showNotification();
    
            Thread thr = new Thread(null, mTask, "RepeatAlarm");
            thr.start();
    
        }
    
        @Override
        public void onDestroy() {
    
            mNM.cancel(R.string.alarm_service_started);
            Toast.makeText(this, R.string.alarm_service_finished, Toast.LENGTH_SHORT).show();
        }
    
        /**
         * The function that runs in our worker thread
         */
        Runnable mTask = new Runnable() {
            public void run() {
    
                long endTime = System.currentTimeMillis() + 15*1000;
                while (System.currentTimeMillis() < endTime) {
                    synchronized (mBinder) {
                        try {
                            mBinder.wait(endTime - System.currentTimeMillis());
    
                        } catch (Exception e) {
                        }
                    }
                }
    
    
                RepeatAlarm.this.stopSelf();
            }
        };
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
    
            return mBinder;
        }
    
        /**
         * Show a notification while this service is running.
         */
        private void showNotification() {
    
            CharSequence text = getText(R.string.alarm_service_started);
    
            Notification notification = new Notification(R.drawable.icon, text,
                    System.currentTimeMillis());
    
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                    new Intent(this, B.class), 0);
    
            // Set default sound
            notification.defaults |= Notification.DEFAULT_SOUND;
            //Set the default Vibration
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            // Set the light pattern
            notification.ledARGB = 0xff00ff00;
            notification.ledOnMS = 300;
            notification.ledOffMS = 1000;
            notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    
    
            //Custom notification view
            RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
            contentView.setImageViewResource(R.id.image, R.drawable.icon);
            contentView.setTextViewText(R.id.text, "Notification");
            notification.contentView = contentView;
    
            notification.contentIntent = contentIntent;
    
            // Send the notification.
            // We use a layout id because it is a unique number.  We use it later to cancel.
            mNM.notify(R.string.alarm_service_started, notification);
        }
    
        /**
         * This is the object that receives interactions from clients.  See RemoteService
         * for a more complete example.
         */
        private final IBinder mBinder = new Binder(){
            @Override
            protected boolean onTransact(int code, Parcel data, Parcel reply,
                    int flags) throws RemoteException {
                    return super.onTransact(code, data, reply, flags);
                }
        };
    }
    

    现在我正尝试尝试第二种情况,因为我在通知布局上放了两个按钮,称为Reschedule和Cancel,但是在这种情况下我的视图没有显示在通知窗口中,我从最近2天开始用Google搜索但是没有运气,现在这对我来说是非常惊人的情况,我必须在一天结束时以任何方式完成它。因此,任何建议或解决方法对我都非常有帮助。

    谢谢

    与Ashish

0 个答案:

没有答案