Android 报警通知

发布于 2024-11-09 21:34:29 字数 4488 浏览 2 评论 0原文

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

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

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

下面是我为用例 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);
            }
    };
}

现在我尝试尝试第二种情况,为此我在通知布局上放置了 2 个按钮,称为“重新安排”和“取消” ,但在这种情况下,我的视图没有显示在通知窗口中,我过去两天已经用谷歌搜索过,但没有运气,现在这对我来说是非常令人震惊的情况,我必须在一天结束之前以任何方式完成它。因此,任何建议或解决方法都将对我非常有帮助。

谢谢阿什什

I am trying to implement a Alarm functionality in my mobile apps. Below is my use case

  1. User can set reminder from Activity "A" once the reminder is set, User will receive a notification 10min before the set time. Now user can remove the reminder from another activity , which is let suppose "B".

  2. Apart from that I also want that user can reschedule his/her reminder setting from notification window.

Below is the code I have written for use case 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);
            }
    };
}

Now I am trying to attempt the 2nd case, for that I put 2 buttons on the notification layout, called Reschedule and Cancel, but in this case my view is not showing in the notification window, I have googled from last 2 days but no-luck, Now it is very alarming situation for me, I have to complete it any way by end of the day. So any suggestion or workaround will extremely helpful for me.

Thanks

Ashish

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文