这是每天5.15 PM设置闹钟的方法
我正在调用setAlarm()
方法,当我的应用程序首次启动时.App会在此时打开(First Launch time)
自动Notification
生成并再次同时再次触发相同的通知并{{1}围绕interval time
。
注意: 生成通知不是问题。
2 min
AlarmService
public void setAlarm() {
alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
alarmIntent = new Intent(getActivity(), AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, alarmIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 15);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
//alarmManager.setRepeating(AlarmManager.RTC, AlarmManager.INTERVAL_HALF_DAY, AlarmManager.INTERVAL_HALF_DAY, pendingIntent);
}
MyAlarmSevice
public class AlarmReceiver extends BroadcastReceiver {
NotificationManager notificationManager;
@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
答案 0 :(得分:3)
对于每日行情,我做了一些事情......
public void setAlarm() {
// Quote in Morning at 08:32:00 AM
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 32);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Calendar cur = Calendar.getInstance();
if (cur.after(calendar)) {
calendar.add(Calendar.DATE, 1);
}
Intent myIntent = new Intent(context, DailyReceiver.class);
int ALARM1_ID = 10000;
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context, ALARM1_ID, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
并获得此reciver
public class DailyReceiver extends BroadcastReceiver {
DatabaseHelper databaseHelper;
@Override
public void onReceive(Context context, Intent intent) {
String quote ;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, DailySpecialActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// get your quote here
quote = doSomeMethod();
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My Quotes")
.setContentText(quote).setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setStyle(new NotificationCompat.BigTextStyle().bigText(quote))
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}); // Declair VIBRATOR Permission in AndroidManifest.xml
notificationManager.notify(5, mNotifyBuilder.build());
}
}
在您重新启动手机时,您还需要重新启动闹钟,所以我已经
了public class WakeUpAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// Quote in Morning
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Calendar cur = Calendar.getInstance();
if (cur.after(calendar)) {
calendar.add(Calendar.DATE, 1);
}
Intent myIntent = new Intent(context, DailyReceiver.class);
int ALARM1_ID = 10000;
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context, ALARM1_ID, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
}
最后这是你的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nils.myquotesapp">
<!--you must need this three permissions-->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:name="com.nils.myquotesapp.QuotesApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.nils.myquotesapp.Activity.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.nils.myquotesapp.DailyReceiver"
android:enabled="true" />
<receiver
android:name="com.nils.myquotesapp.WakeUpAlarmReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</manifest>
至于最好的运气兄弟...... !!
答案 1 :(得分:2)
您可以执行这些日常任务,例如警报管理器和其他具有以下服务的任务。
以下可能更有用,更节省电池并优化使用。
1:JobSchedular
2:ScheduledExecutorService
虽然它是由android支持库提供的,但您可以在互联网上找到非常有用的示例。
快乐编码:)