[embedyt] https://www.youtube.com/watch?v=yrpimdBRk5Q[/embedyt]
activity_main.xmlMainActivity.javaAlertReceiver.javaAndroidManifest.xmlNotificationHelper.javaTimePickerFragment.java
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.application.alarmmanagerproject.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/button_timepicker" android:layout_centerHorizontal="true" android:layout_marginBottom="30dp" android:text="No Alarm set" android:textSize="30sp" /> <Button android:id="@+id/button_timepicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="open time picker" /> <Button android:id="@+id/button_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button_timepicker" android:layout_centerHorizontal="true" android:layout_marginTop="27dp" android:text="cancel alarm" /> </RelativeLayout>
package com.example.application.alarmmanagerproject; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.Context; import android.content.Intent; import android.support.v4.app.DialogFragment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; import java.text.DateFormat; import java.util.Calendar; public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener { private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = findViewById(R.id.textView); Button buttonTimePicker = findViewById(R.id.button_timepicker); buttonTimePicker.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DialogFragment timePicker = new TimePickerFragment(); timePicker.show(getSupportFragmentManager(), "time picker"); } }); Button buttonCancelAlarm = findViewById(R.id.button_cancel); buttonCancelAlarm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cancelAlarm(); } }); } @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 0); updateTimeText(c); startAlarm(c); } private void updateTimeText(Calendar c) { String timeText = "Alarm set for: "; timeText += DateFormat.getTimeInstance(DateFormat.SHORT).format(c.getTime()); mTextView.setText(timeText); } private void startAlarm(Calendar c) { AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, AlertReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0); if (c.before(Calendar.getInstance())) { c.add(Calendar.DATE, 1); } alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent); } private void cancelAlarm() { AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, AlertReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0); alarmManager.cancel(pendingIntent); mTextView.setText("Alarm canceled"); } }
package com.example.application.alarmmanagerproject; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; public class AlertReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { NotificationHelper notificationHelper = new NotificationHelper(context); NotificationCompat.Builder nb = notificationHelper.getChannelNotification(); notificationHelper.getManager().notify(1, nb.build()); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.application.alarmmanagerproject"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".AlertReceiver" /> </application> </manifest>
package com.example.application.alarmmanagerproject; import android.annotation.TargetApi; import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; import android.content.ContextWrapper; import android.os.Build; import android.support.v4.app.NotificationCompat; public class NotificationHelper extends ContextWrapper { public static final String channelID = "channelID"; public static final String channelName = "Channel Name"; private NotificationManager mManager; public NotificationHelper(Context base) { super(base); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createChannel(); } } @TargetApi(Build.VERSION_CODES.O) private void createChannel() { NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH); getManager().createNotificationChannel(channel); } public NotificationManager getManager() { if (mManager == null) { mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } return mManager; } public NotificationCompat.Builder getChannelNotification() { return new NotificationCompat.Builder(getApplicationContext(), channelID) .setContentTitle("Alarm!") .setContentText("Your AlarmManager is working.") .setSmallIcon(R.drawable.ic_android); } }
package com.example.application.alarmmanagerproject; import android.app.Dialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.text.format.DateFormat; import java.util.Calendar; public class TimePickerFragment extends DialogFragment { @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, DateFormat.is24HourFormat(getActivity())); } }