我查看了整个代码,一切似乎都正常工作,没有错误,这使得找到解决方案变得更加困难。
我已将我的代码移到另一个项目中。
这是我的广播接收器类:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle(intent.getStringExtra("Next"));
builder.setContentText(intent.getStringExtra("String"));
builder.setSmallIcon(R.drawable.notify);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(325, builder.build());
}
}
我在我的主要活动中称之为:
private void notifyUser() {
Intent intent = new Intent(this, MyBroadcastReceiver.class);
intent.putExtra("Next", getResources().getString(R.string.lesson_next));
int days_int = (sun) ? 1:2;
for (int i = 0; i < days_length; i++) {
Calendar[] calendars = getCalendars(i, days_int + i);
ArrayList<String> arrayList = schedules.get(i);
for (int j = 0; j < calendars.length; j++) {
String s = arrayList.get(j);
if (!s.equals(getResources().getString(R.string.lesson_window))) {
intent.putExtra("String", s);
notifyUserTag(intent, 135124 + count, calendars[j].getTimeInMillis());
count++;
}
}
}
}
private void notifyUserTag(Intent intent, int id, long currentTimeMillis) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), id, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, currentTimeMillis,
AlarmManager.INTERVAL_DAY * 7, pendingIntent);
}
private Calendar[] getCalendars(int i, int day) {
int[] nums = lessonNum(schedules.get(i)); int len = nums.length;
Calendar[] calendars = new Calendar[len];
for (int k = 0; k < len; k++)
calendars[k] = setCalendar(nums[k], day);
return calendars;
}
private int[] lessonNum(ArrayList<String> stringArrayList) {
int size = stringArrayList.size();
int[] nums = new int[size];
for (int i = 0; i < size; i++)
nums[i] = lessonNumTry(i, stringArrayList);
return nums;
}
private int lessonNumTry(int i, ArrayList<String> arrayList) {
try {
if (Integer.parseInt(arrayList.get(i).substring(1,2)) == 0)
return 10;
}
catch (NumberFormatException e) {
return Integer.parseInt(arrayList.get(i).substring(0,1));
}
return -1;
}
private Calendar setCalendar(int i, int day) {
Calendar calendar = Calendar.getInstance();
switch (i) {
case 0:
calHelper(calendar, day, 6, 0);
break;
case 1:
calHelper(calendar, day, 7, 0);
break;
case 2:
calHelper(calendar, day, 8, 0);
break;
case 3:
calHelper(calendar, day, 8, 55);
break;
case 4:
calHelper(calendar, day, 9, 50);
break;
case 5:
calHelper(calendar, day, 10, 55);
break;
case 6:
calHelper(calendar, day, 11, 50);
break;
case 7:
calHelper(calendar, day, 12, 45);
break;
case 8:
calHelper(calendar, day, 13, 40);
break;
case 9:
calHelper(calendar, day, 14, 25);
break;
case 10:
calHelper(calendar, day, 15, 15);
break;
}
return calendar;
}
private void calHelper(Calendar calendar, int day, int hour, int minute) {
long currTime = calendar.getTimeInMillis();
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
long getTime = calendar.getTimeInMillis();
if (getTime < currTime)
calendar.setTimeInMillis(getTime + 604800000);
}
发送通知的函数(notifyUser())应该获得一组日历,每个日历指定一天/小时/分钟,并设置警报以在那时每周启动广播接收器。我在旧项目中使用了完全相同的代码并且它工作正常,但现在却没有。我的旧项目和新项目之间的主要变化是新的使用数据库来存储数据(sqlite3)而旧的使用文件,我不认为这与广播接收器不是这样的事实相关被呼叫/显示/正在进行的任何事情。
在清单中我有这个
<receiver android:name=".MyBroadcastReceiver" />
任何帮助将不胜感激!
答案 0 :(得分:0)
如果你在清单中说明
<receiver android:name=".MyBroadcastReceiver" />
MyBroadcastReceiver必须位于代码根目录中,而不是位于子包中。它在根包中吗?您可能希望在清单中输入整个包名称,例如:
<receiver android:name="org.myapp.receivers.MyBroadcastReceiver" />
答案 1 :(得分:0)
您尚未使用sendBroadcast(intent);
答案 2 :(得分:0)
您在定义意图后错过了发送广播意图。在定义你的意图后添加这一行:
sendBroadcast(intent);
答案 3 :(得分:0)
在清单中注册具有以下意图过滤器的接收器。
<action android:name="android.intent.action.BOOT_COMPLETED"/>