我创建了一个应用程序,在启动完成后启动一个意图、一个警报管理器和一个通知栏。重新启动后,通知类运行,但在固定时间我没有在栏上看到通知。
另一个问题:我可以从 Notify 类启动通知栏而不调用 AlarmReceiver 类吗?
自动启动
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);
报警接收器:
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>