我创建了一个应用程序,它在启动完成后启动一个意图,一个警报管理器,然后是一个通知栏。重启后,Notify类运行,但在固定的时间,我没有看到栏上的通知。
另一个问题:我可以在不调用AlarmReceiver类的情况下从Notify类启动通知栏吗?
AutoStart
public class AutoStart extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, Notify.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}通知
public class Notify extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 05);
cal1.set(Calendar.MINUTE, 45);
cal1.set(Calendar.SECOND, 00);
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.HOUR_OF_DAY, 17);
cal2.set(Calendar.MINUTE, 30);
cal2.set(Calendar.SECOND, 00);
Calendar now = Calendar.getInstance();
if(now.after(cal1))
cal1.add(Calendar.HOUR_OF_DAY, 24);
if(now.after(cal2))
cal2.add(Calendar.HOUR_OF_DAY, 24);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent morningAlarm = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
PendingIntent eveningAlarm = PendingIntent.getBroadcast(getApplicationContext(), 1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, morningAlarm);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, eveningAlarm);AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
final String not1[] = new String[37];
int not1end = 36;
int x;
public void onReceive(Context context, Intent intent) {
not1[0]="one";
not1[1]="two";
not1[2]="three";
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Votre vidange approche";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "Notification";
CharSequence contentText = "Vérifier votre kilométrage";
Intent notificationIntent = new Intent("org.gortcloud.perledisaggezza");
// PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
} 清单
<receiver
android:name=".Notify"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name="AlarmReceiver" >
<intent-filter>
<action android:name="com.example.AlarmReceiver" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>发布于 2012-12-19 22:20:10
在AlarmReceiver.onReceive()中,您可以创建Notification对象,但从不将其排队。您需要像这样调用NotificationManager.notify():
mNotificationManager.notify(MY_NOTIFICATION_ID, notification);其中MY_NOTIFICATION_ID是任何整数值。稍后,如果您需要取消通知,您将使用该值,如下所示:
mNotificationManager.cancel(MY_NOTIFICATION_ID);https://stackoverflow.com/questions/13953887
复制相似问题