显示已编程警报的界面

时间:2019-05-24 00:16:40

标签: java android

我有一个代码,可以设置警报,但是我希望您向我展示所有已编程的警报并进行编辑,在浏览google时我什么也没找到,因此,即使您给我提供了手册或重要帮助,给我一个如何做的想法。如果您需要android清单,并且主要活动保留在注释中以添加它,也感谢您的帮助。该应用程序的目的是制作一个交互式警报,用户需要执行一些操作来使警报无效(如果有人帮助或提出一个值得赞赏的想法)。谢谢

   package com.example.alarm;

    import android.app.Activity;
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.TimePicker;
    import android.widget.ToggleButton;

    import java.util.Calendar;

    public class AlarmActivity extends Activity {

        AlarmManager alarmManager;
        private PendingIntent pendingIntent;
        private TimePicker alarmTimePicker;
        private static AlarmActivity inst;
        private TextView alarmTextView;

        public static AlarmActivity instance() {
            return inst;
        }

        @Override
        public void onStart() {
            super.onStart();

            inst = this;
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
            alarmTextView = (TextView) findViewById(R.id.alarmText);
            ToggleButton alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle);
            alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        }

        public void onToggleClicked(View view) {
            if (((ToggleButton) view).isChecked()) {
                Log.d("MyActivity", "Alarm On");
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
                calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
                Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class);
                pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, myIntent, 0);
                alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
            } else {
                alarmManager.cancel(pendingIntent);
                setAlarmText("");
                Log.d("MyActivity", "Alarm Off");
            }
        }

        public void setAlarmText(String alarmText) {
            alarmTextView.setText(alarmText);
        }
    }

    ```
    ```
    package com.example.alarm;

    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.media.Ringtone;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.support.v4.content.WakefulBroadcastReceiver;

    public class AlarmReceiver extends WakefulBroadcastReceiver {

        @Override
        public void onReceive(final Context context, Intent intent) {
            //this will update the UI with message |
            AlarmActivity inst = AlarmActivity.instance();
            inst.setAlarmText("Alarm! Wake up! Wake up!");

            //this will sound the alarm tone
            //this will sound the alarm once, if you wish to
            //raise alarm in loop continuously then use MediaPlayer and setLooping(true)
            Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
            if (alarmUri == null) {
                alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            }
            Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
            ringtone.play();

            //this will send a notification message
            ComponentName comp = new ComponentName(context.getPackageName(),
                    AlarmService.class.getName());
            startWakefulService(context, (intent.setComponent(comp)));
            setResultCode(Activity.RESULT_OK);
        }
    }

    ```
    package com.example.alarm;

    import android.app.IntentService;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.support.v4.app.NotificationCompat;
    import android.util.Log;

    public class AlarmService extends IntentService {
        private NotificationManager alarmNotificationManager;

        public AlarmService() {
            super("AlarmService");
        }

        @Override
        public void onHandleIntent(Intent intent) {
            sendNotification("Wake Up! Wake Up!");
        }

        private void sendNotification(String msg) {
            Log.d("AlarmService", "Preparing to send notification...: " + msg);
            alarmNotificationManager = (NotificationManager) this
                    .getSystemService(Context.NOTIFICATION_SERVICE);

            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                    new Intent(this, AlarmActivity.class), 0);

            NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
                    this).setContentTitle("Alarm").setSmallIcon(R.drawable.ic_launcher)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                    .setContentText(msg);


            alamNotificationBuilder.setContentIntent(contentIntent);
            alarmNotificationManager.notify(1, alamNotificationBuilder.build());
            Log.d("AlarmService", "Notification sent.");
        }
    }

0 个答案:

没有答案