我正在编写一个应用程序,其中我使用两个选项卡,一个用于获取 facebook 朋友列表,第二个选项卡用于获取电话簿朋友列表
在电话簿朋友选项卡中,我还会收到朋友的生日并为此显示提醒,如果我单独尝试,我也可以收到提醒,无需与另一个选项卡组合(即:使用 Facebook 选项卡)
这意味着一旦我将它组合到选项卡中,我就不会收到任何生日通知。
我正在使用以下课程来获取电话簿朋友生日的通知:
public class BirthdayReminder extends ListActivity {
// TODO: call/write message on birthday
// TODO: hideNotificationPref
private final DateFormatSymbols dateSymbols = new DateFormatSymbols();
private Database db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setFastScrollEnabled(true);
this.db = new Database(getContentResolver());
// debug code
// Debug.logDatabase(this);
// start BirthdayBroadcastReceiver if it is activated
Preferences prefs = Preferences.getInstance(getApplicationContext());
if (prefs.getActivateService()) {
BirthdayBroadcastReceiver.restart(getApplicationContext());
}
}
@Override
protected void onResume() {
super.onResume();
updateView();
}
private void updateView() {
// create new list adapter
MultiListAdapter listAdapter = new MultiListAdapter();
List<ListAdapter> adapterList = listAdapter.getListAdapters();
// load birthday and contact information
List<Contact> contacts = this.db.getAllContacts();
List<BirthContact> birthContacts = BirthContactHelper.createBirthContactList(contacts);
// group all contacts by known and unknown birthday
SortedSet<BirthContact> knownBirthdays = new TreeSet<BirthContact>(new BirthContactBirthdayComparator());
SortedSet<BirthContact> unknownBirthdays = new TreeSet<BirthContact>(new BirthContactNameComparator());
for (BirthContact birthContact : birthContacts) {
DateOfBirth dateOfBirth = birthContact.getDateOfBirth();
if (dateOfBirth != null) {
knownBirthdays.add(birthContact);
} else {
unknownBirthdays.add(birthContact);
}
}
Integer currentMonth = null;
BirthContactAdapter currentBirthContactAdapter = null;
String[] monthStrs = this.dateSymbols.getMonths();
for (BirthContact birthContact : knownBirthdays) {
int month = birthContact.getDateOfBirth().getDate().get(Calendar.MONTH);
if (currentMonth == null || currentMonth != month) {
currentMonth = month;
currentBirthContactAdapter = new BirthContactAdapter(this);
adapterList.add(new CategoryAdapter(this, monthStrs[currentMonth]));
adapterList.add(currentBirthContactAdapter);
}
currentBirthContactAdapter.add(birthContact);
}
adapterList.add(new CategoryAdapter(this, getResources().getString(R.string.unknownBirthdays)));
adapterList.add(new BirthContactAdapter(this, unknownBirthdays));
setListAdapter(listAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
BirthContact birthContact = (BirthContact) l.getAdapter().getItem(position);
Intent editorIntent = new Intent(this, BirthdayEditor.class);
editorIntent.putExtra(BirthdayEditor.CONTACT_ID, birthContact.getContact().getId());
startActivity(editorIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.preferences:
startActivity(new Intent(this, PreferenceWindow.class));
return true;
case R.id.quit:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
我正在使用下面的代码在单击特定选项卡时调用两个不同的类。
但是每当我将其更改为选项卡时,我都没有收到任何通知 TabSample.java:
public class TabSample extends TabActivity {
String response;
@SuppressWarnings("unused")
private static JSONArray jsonArray;
public static TabHost mTabHost;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
response = getIntent().getStringExtra("FRIENDS");
try {
jsonArray = new JSONArray(response);
} catch (JSONException e) {
FacebookUtility.displayMessageBox(this,
this.getString(R.string.json_failed));
}
setTabs();
}
private void setTabs() {
addTab("", R.drawable.tab_menu, com.chr.tatu.sample.friendslist.facebook.FriendsList.class);
addTab("", R.drawable.tab_contact, com.chr.tatu.sample.friendslist.contacts.BirthdayReminder.class);
}
通知:
public class BirthdayBroadcastReceiver extends BroadcastReceiver {
private static final String TIMED = "timed";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getBooleanExtra(TIMED, false)) {
notifyBirthdays(context);
}
start(context);
}
private static PendingIntent createPendingIntent(Context context) {
Intent intent = new Intent(context, BirthdayBroadcastReceiver.class);
intent.putExtra(TIMED, true);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void start(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = createPendingIntent(context);
Preferences prefs = Preferences.getInstance(context);
Time nextUpdateTime = prefs.getUpdateTime();
Calendar wakeUpCal = Calendar.getInstance();
wakeUpCal.set(Calendar.HOUR_OF_DAY, nextUpdateTime.getHours());
wakeUpCal.set(Calendar.MINUTE, nextUpdateTime.getMinutes());
wakeUpCal.set(Calendar.SECOND, wakeUpCal.getActualMinimum(Calendar.SECOND));
wakeUpCal.set(Calendar.MILLISECOND, wakeUpCal.getActualMinimum(Calendar.MILLISECOND));
if (wakeUpCal.before(Calendar.getInstance())) {
wakeUpCal.add(Calendar.DAY_OF_MONTH, 1);
}
alarmManager.set(AlarmManager.RTC_WAKEUP, wakeUpCal.getTimeInMillis(), pendingIntent);
}
public static void stop(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = createPendingIntent(context);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
}
public static void restart(Context context) {
stop(context);
start(context);
}
private void notifyBirthdays(Context context) {
Calendar today = CalendarUtils.todaysCalendar();
Database db = new Database(context.getContentResolver());
Preferences prefs = Preferences.getInstance(context);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = context.getResources();
List<Contact> contacts = db.getAllContacts();
// calculate next birthdays
SortedMap<Integer, List<String>> nextBirthdays = new TreeMap<Integer, List<String>>();
for (Contact contact : contacts) {
Integer timeSpanToNextBirthday = null;
for (RawContact rawContact : contact.getRawContacts()) {
for (DateOfBirth dateOfBirth : rawContact.getDatesOfBirth()) {
int timeSpan = CalendarUtils.timeSpanToNextBirthday(today, dateOfBirth.getDate());
if (timeSpanToNextBirthday == null || timeSpanToNextBirthday > timeSpan) {
timeSpanToNextBirthday = timeSpan;
}
}
}
if (timeSpanToNextBirthday != null && timeSpanToNextBirthday <= prefs.getDaysBeforeReminder()) {
List<String> infoNames = nextBirthdays.get(timeSpanToNextBirthday);
if (infoNames == null) {
infoNames = new ArrayList<String>();
nextBirthdays.put(timeSpanToNextBirthday, infoNames);
}
infoNames.add(contact.getName());
}
}
// collect all sentences for the notification
List<String> notificationTexts = new ArrayList<String>();
int countBirthdays = 0;
for (Integer days : nextBirthdays.keySet()) {
List<String> birthdayList = nextBirthdays.get(days);
String names = StringUtils.join(birthdayList, ", ").toString();
notificationTexts.add(getBirthdayText(res, days, names));
countBirthdays += birthdayList.size();
}
// cancel all notifications (clear old once)
notificationManager.cancelAll();
// create new notification
if (notificationTexts.size() > 0) {
String titleText = String.format(res.getQuantityString(R.plurals.notificationTitle, countBirthdays),
countBirthdays);
Intent intent = new Intent(context, BirthdayReminder.class);
Notification notification = new Notification(R.drawable.balloons2, titleText, System.currentTimeMillis());
if (countBirthdays > 1) {
notification.number = countBirthdays;
}
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, titleText, StringUtils.join(notificationTexts, ", "), pi);
notificationManager.notify(0, notification);
}
}
private String getBirthdayText(Resources res, int days, String names) {
String text;
switch (days) {
case 0:
text = String.format(res.getString(R.string.notificationText_today), names);
break;
case 1:
text = String.format(res.getString(R.string.notificationText_tomorrow), names);
break;
default:
text = String.format(res.getString(R.string.notificationText_other), days, names);
}
return text;
}
}