我想让应用在一天的特定时间发送通知,我需要使用带有通知的闹钟管理器,如tutorial所述。
代码运行良好,并在指定的时间发送通知,即使应用程序没有运行,但是在我运行应用程序的任何时候发送通知都非常奇怪..下面是代码,感谢任何暗示。
onCreate()
MainActivity
内的
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, Calendar.MAY);
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.DAY_OF_MONTH, 27);
calendar.set(Calendar.HOUR_OF_DAY,10);
calendar.set(Calendar.MINUTE, 33);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);
Intent myIntent = new Intent(this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
MyReceiver.java
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
MyAlarmService
public class MyAlarmService extends Service
{
private NotificationManager mManager;
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
@SuppressWarnings({ "static-access"})
@Override
public void onStart(Intent intent, int startId)
{
mManager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Today 27-5-2014")
.setContentText(" You have your last final exam ")
.setContentIntent(pendingNotificationIntent);
mManager.notify(0, mBuilder.build());
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
}
编辑:
请注意我的要求是在特定时间发送通知说在上午10点... 上面的代码成功在上午10:00发送通知, 我的问题是,每次运行应用程序时,即使不是上午10:00,通知仍会被触发
答案 0 :(得分:0)
尝试以下代码..
public class MainActivity extends Activity {
Calendar calendar;
private PendingIntent alarmIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calendar = Calendar.getInstance();
Long time = new GregorianCalendar().getTimeInMillis()+60*06*24*1000;
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmIntent=PendingIntent.getBroadcast(this, 1, intentAlarm, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,time, alarmIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000 * 60*60*24, alarmIntent);
}
}
AlarmReceiver.java
公共类AlarmReceiver扩展了BroadcastReceiver {
private static final int MY_NOTIFICATION_ID=1;
Intent in;
PendingIntent pendingIntent;
Notification mBuilder;
@Override
public void onReceive(Context context, Intent intent)
{
in=new Intent(context,MainActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pendingIntent=PendingIntent.getActivity(context, 0, in, 0);
mBuilder=new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Your Title")
.setContentText("Your Text")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder);
mNotificationManager.notify(MY_NOTIFICATION_ID, mBuilder);
}
}
在清单文件中包含以下代码
<application>
....
<receiver android:name=".AlarmReceiver"/>
</application>
AlarmManager的简易教程here ...
答案 1 :(得分:0)
我尝试了以下代码,每24小时发出一次警报。它工作正常,但是当您第一次启动应用程序时,您将看到通知。但是稍后它会在你打开App时不会产生通知。
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, PendingIntent.FLAG_NO_CREATE);
boolean testval = (pendingIntent == null);
if(testval)
{
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}