问题
Ok.. I'm trying to figure out three problems.
- I am unable to make AlarmManager notify me
- I want to display a different message for each notifier
I will be glad if anyone can help me out, I'm not posting this here just so I can get the code, I have been at it for 2 days so I thought I'd ask the experts here.
If I can find someone who would check the below code, show my wrongs and point me in the direction to rectify those wrongs.. Thank yhu.
Here is my Notifier class where I set the days and time I want the alarmManager to send an alarm.
public class Notifier extends Activity {
private PendingIntent pendingIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_beginning);
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
Calendar calendar3 = Calendar.getInstance();
Calendar calendar4 = Calendar.getInstance();
calendar1.set(Calendar.DAY_OF_WEEK, 1); // Sunday
calendar2.set(Calendar.DAY_OF_WEEK, 4); // Wednesday
calendar3.set(Calendar.DAY_OF_WEEK, 6); // Friday
calendar4.set(Calendar.DAY_OF_WEEK, 5); // Thursday
calendar4.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1); // First Thursday of Each Month
// Sunday
calendar1.set(Calendar.HOUR_OF_DAY, 5);
calendar1.set(Calendar.MINUTE, 0);
calendar1.set(Calendar.SECOND, 0);
calendar1.set(Calendar.AM_PM, Calendar.PM);
// Wednesday
calendar2.set(Calendar.HOUR_OF_DAY, 17);
calendar2.set(Calendar.MINUTE, 30);
calendar2.set(Calendar.SECOND, 0);
calendar2.set(Calendar.AM_PM, Calendar.PM);
// Friday
calendar3.set(Calendar.HOUR_OF_DAY, 17);
calendar3.set(Calendar.MINUTE, 30);
calendar3.set(Calendar.SECOND, 0);
calendar3.set(Calendar.AM_PM, Calendar.PM);
// Thursday
calendar4.set(Calendar.HOUR_OF_DAY, 9);
calendar4.set(Calendar.MINUTE, 0);
calendar4.set(Calendar.SECOND, 0);
calendar4.set(Calendar.AM_PM, Calendar.AM);
Intent myIntent = new Intent(Notifier.this, MyBReceiver.class);
pendingIntent = PendingIntent.getBroadcast(Notifier.this, 0, myIntent,
0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar1.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
pendingIntent); // every Sunday
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar2.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
pendingIntent); // every Wednesday
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar3.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
pendingIntent); // every Friday
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar4.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 28,
pendingIntent); // every first Thursday
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar4.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 30,
pendingIntent);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar4.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 31,
pendingIntent);
} // end onCreate
}
Here is My Receiver class
public class MyBReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent myservice = new Intent(context, MyAlarmService.class);
context.startService(myservice);
}
}
and here is my Alarmservice to set up the notifiers (There is just one notifier here) I do not know how to set up another notifier for the next three alarms.
public class MyAlarmService extends Service {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
private NotificationManager mManager;
private Context context;
// Notification notification;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
onStartCommand(intent, 0, startId);
}
@SuppressWarnings({ "static-access", "deprecation" })
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mManager = (NotificationManager) context
.getSystemService(context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),
Notifier.class);
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
Notification notification = new Notification(
R.drawable.ic_launcher, "This is a test message!",
System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent
.getActivity(this.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(),
"AlarmManagerDemo", "This is a test message!",
pendingNotificationIntent);
mManager.notify(0, notification);
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("AlarmManagerDemo1")
.setContentText("This is a test message");
Intent notificationIntent = new Intent(this, Notifier.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
Thank you.
回答1:
use a different myIntent for each alarm. also, to avoid android optimizing it, add a different data to the intent
myIntent = new Intent(Notifier.this, MyBReceiver.class);
data = Uri.withAppendedPath(Uri.parse("anything" + "://something/different");
myIntent.setData(data);
来源:https://stackoverflow.com/questions/20445481/cant-get-alarmmanager-and-multiple-notifications-to-work