我真的想知道如何为API 23,19做一个重复警报 我搜索了很多,但我无法理解如何制作它。
我得到的是使用:
alarmManger.setExactAndAllowWholeIdle
- >对于api 23
而
SetExact
- >适用于API 19及更多
但我不知道如何重复它们!
和
Setrepeating
- >适用于API 14及更多
API 23 AND 19的问题
所以,如果有人可以通过一个例子来帮助我。
答案 0 :(得分:1)
您可以创建一个可运行的类来执行警报管理器类的待处理意图:
public class Alarm_task implements Runnable {
// The date selected for the alarm
private final Calendar cal;
// The android system alarm manager
private final AlarmManager am;
// Your context to retrieve the alarm manager from
private final Context context;
long alarm_time2;
int _id;
public Alarm_task(Context context, Calendar cal) {
this.context = context;
this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
this.cal = cal;
this._id = (int) System.currentTimeMillis();
alarm_time2 = cal.getTimeInMillis();
//Toast.makeText(getActivity(), alarm_time2 + " ", Toast.LENGTH_SHORT).show();
}
@Override
public void run() {
// Request to start are service when the alarm date is upon us
// We don't start an activity as we just want to pop up a notification into the system bar not a full activity
Intent i = new Intent("com.package_name.alarm");
i.setAction("com.package_name.alarm");
/** Creating a Pending Intent */
PendingIntent operation = PendingIntent.getActivity(getActivity(), _id, i, PendingIntent.FLAG_UPDATE_CURRENT);
/** Converting the date and time in to milliseconds elapsed since epoch */
long alarm_time = cal.getTimeInMillis();
//Toast.makeText(getBaseContext(), alarm_time+" " ,Toast.LENGTH_SHORT).show();
/** Setting an alarm, which invokes the operation at alart_time */
// am.set(AlarmManager.RTC_WAKEUP, alarm_time, operation);
//this line will repeat the alarm in 24 hour interval
am.setRepeating(AlarmManager.RTC_WAKEUP, alarm_time, AlarmManager.INTERVAL_DAY, operation);
}
}
现在调用此警报任务:
//here set your desired time for the alarm
String dateString = "11/21/2016 11:49:00 AM";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
Date date = dateFormat.parse(dateString);
Calendar calNow = Calendar.getInstance();
calNow.setTime(date);
long current_time = calNow.getTimeInMillis();
new Alarm_task(getActivity(), calNow).run();
您可以定义一个类来处理清单文件中的特定待处理意图
<activity
android:name="com.package_name.Prereminder"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.package_name.alarm" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
现在,在预备者类中,您可以在此期间显示对话框或显示通知: Prereminder.java
public class Prereminder extends FragmentActivity {
String msg = "Alarm Title";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
sendNotification();
/** Creating an Alert Dialog Window */
Reminder_alert alert = new Reminder_alert();
/** Opening the Alert Dialog Window */
alert.show(getSupportFragmentManager(), "Reminder_alert");
}
private void sendNotification() {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
//handle notification onClick
Intent myintent = new Intent(this, Home_page.class);
myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo_small)
.setContentTitle("Alarm alert")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
在Reminder_alert.java中显示对话框消息
public class Reminder_alert extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
/** Turn Screen On and Unlock the keypad when this alert dialog is displayed */
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
/** Creating a alert dialog builder */
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
/** Setting title for the alert dialog */
builder.setTitle("Alarm");
/** Setting the content for the alert dialog */
builder.setMessage("Alarm alert");
/** Defining an OK button event listener */
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
/** Creating the alert dialog window */
return builder.create();
}
/**
* The application should be exit, if the user presses the back button
*/
@Override
public void onDestroy() {
super.onDestroy();
getActivity().finish();
}
}
答案 1 :(得分:0)
我通过使用服务
解决了这个问题这是我的代码的一部分
日历日历= Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
Intent intent1 = new Intent(getApplicationContext(), AlarmAzkarBroad.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), RC, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, hour, AlarmManager.INTERVAL_HOUR, pendingIntent);
开始广播的意图