Android 安卓警报通知

Android 安卓警报通知,android,notifications,alarm,Android,Notifications,Alarm,我正在尝试在我的移动应用程序中实现报警功能。下面是我的用例 用户可以从活动“A”设置提醒设置提醒设置提醒后,用户将在设置时间前10分钟收到通知。现在,用户可以从另一个活动中删除提醒,假设“B” 除此之外,我还希望用户可以从通知窗口重新安排他/她的提醒设置 下面是我为用例1编写的代码 private void setAlarm(){ Intent intent = new Intent(A.this, RepeatAlarm.class); mAlarmSender = Pend

我正在尝试在我的移动应用程序中实现报警功能。下面是我的用例

  • 用户可以从活动“A”设置提醒设置提醒设置提醒后,用户将在设置时间前10分钟收到通知。现在,用户可以从另一个活动中删除提醒,假设“B”

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

  • 下面是我为用例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);
                }
        };
    }
    
    公共类RepeatAlarm扩展服务{
    通知经理mNM;
    @凌驾
    public void onCreate(){
    mNM=(NotificationManager)getSystemService(通知服务);
    showNotification();
    Thread thr=新线程(null,mTask,“RepeatAlarm”);
    thr.start();
    }
    @凌驾
    公共空间{
    mNM.cancel(R.string.alarm\u service\u启动);
    Toast.makeText(this,R.string.alarm\u service\u finished,Toast.LENGTH\u SHORT).show();
    }
    /**
    *在工作线程中运行的函数
    */
    Runnable mTask=new Runnable(){
    公开募捐{
    long-endTime=System.currentTimeMillis()+15*1000;
    while(System.currentTimeMillis()
    现在我尝试尝试第二种情况,因为我在通知布局上放置了两个按钮,称为“重新安排”和“取消”,但在这种情况下,我的视图没有显示在通知窗口中,我在谷歌上搜索了过去两天,但没有运气,现在这对我来说是非常令人担忧的情况,我必须在一天结束前以任何方式完成它。因此,任何建议或解决方法都将对我非常有帮助

    谢谢


    Ashish

    除了你发布的代码之外,你还试过其他东西吗?您是否在调试器中运行了代码?正在调用showNotification()方法吗?您是否尝试过显示一个简单的通知?除了您发布的代码之外,您是否尝试过其他内容?您是否在调试器中运行了代码?正在调用showNotification()方法吗?是否尝试显示一个简单的通知?