Reputation: 8828
I have created a Alarm using AlarmManager.
Intent intent = new Intent(MyApp.this,NotificationMessage.class);
PendingIntent sender = PendingIntent.getBroadcast(MyApp.this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, nextAlarmTime,sender);
Here is NotificationMessage class.
public class NotificationMessage extends BroadcastReceiver {
// Display an alert that we've received a message.
// @Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent();
myIntent.setClass(context, ScheduleAlert.class);
myIntent.setAction(ScheduleAlert.class.getName());
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(myIntent);
}
}
It is calling a Intent for creating a Notification. For getting Notification text, I have to access the database. I want to create sound and vibration for notification. And also show the notification icon at top bar, but no view. But it is showing a black screen at the time of notification. How to solve it?
public class ScheduleAlert extends Activity {
private String notificationAlart;
// ************* Notification and AlarmManager ************//
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get contentText from the database
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Notification notifyDetails = new Notification(
R.drawable.icon, "MyApp", nextAlarmTime);
Context context = getApplicationContext();
CharSequence contentTitle = "MyApp";
CharSequence contentText = notificationAlart;
Intent notifyIntent = new Intent(context, MyApp.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
ScheduleAlert.this, 0, notifyIntent,
android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
notifyDetails.setLatestEventInfo(context, contentTitle,
contentText, pendingIntent);
notifyDetails.flags = Notification.FLAG_AUTO_CANCEL;
notifyDetails.defaults |= Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE;
mNotificationManager.notify((int) editEventid, notifyDetails);
Log.d(null,"notification set");
}
}
Upvotes: 0
Views: 1259
Reputation: 1733
You can't start your activity from the onReceive method. You have to use again the notification manager and create another PendingIntent that describes what is gonna happen when the user click on the Notification.
Consider using the buzzbox api to make your life easier. You have to create a Task and putting your DB code in the doWork method. Then you can schedule your Task using a cron string..
More info: http://hub.buzzbox.com/android-sdk/
Upvotes: 1
Reputation: 63303
Your code is showing a blank screen because you launched an Activity as a result of the alarm firing, and an Activity without a contentView will still take the full screen, but it will be blank. You should be constructing and firing your Notification
directly in the BroadcastReceiver
, not spawning other system components.
public class NotificationMessage extends BroadcastReceiver {
// Display an alert that we've received a message.
public void onReceive(Context context, Intent intent) {
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
Notification notifyDetails = new Notification(R.drawable.icon, "MyApp", nextAlarmTime);
CharSequence contentTitle = "MyApp";
CharSequence contentText = notificationAlart;
Intent notifyIntent = new Intent(context, MyApp.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
ScheduleAlert.this, 0, notifyIntent,
android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
notifyDetails.setLatestEventInfo(context, contentTitle,
contentText, pendingIntent);
notifyDetails.flags = Notification.FLAG_AUTO_CANCEL;
notifyDetails.defaults |= Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE;
mNotificationManager.notify((int) editEventid, notifyDetails);
Log.d(null,"notification set");
}
}
Notice that you are provided with a context when this method is called, so you don't have to spawn an Activity or Service just for the purposes of gaining access to one.
The ONLY exception to this is if the construction of the Notification
is expected to take a long time (with your DB access), in which case you should spawn an IntentService
from onReceive()
to do the work.
Hope that Helps!
Upvotes: 1