I have tried to pull out the notification service from the API's and have it run alone to no avail. When I click the notifyStart button it does not go into the NotifyService class.
public class NotifyWords extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("NOTIFYWORDS", "ONCREATE");
setContentView(R.layout.notifying_controller);
Button button = (Button) findViewById(R.id.notifyStart);
button.setOnClickListener(mStartListener);
button = (Button) findViewById(R.id.notifyStop);
button.setOnClickListener(mStopListener);
}
private OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
startService(new Intent(NotifyWords.this,
NotifyingService.class));
}
};
private OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
stopService(new Intent(NotifyWords.this,
NotifyingService.class));
}
};
}
public class NotifyingService extends Service {
// Use a layout id for a unique identifier
private static int MOOD_NOTIFICATIONS = R.layout.status_bar_notifications;
// variable which controls the notification thread
private ConditionVariable mCondition;
@Override
public void onCreate() {
Log.d("INSIDE ONCREATE SERVICE", "ONCREATE");
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block.
Thread notifyingThread = new Thread(null, mTask, "NotifyingService");
mCondition = new ConditionVariable(false);
notifyingThread.start();
}
@Override
public void onDestroy() {
// Cancel the persistent notification.
mNM.cancel(MOOD_NOTIFICATIONS);
// Stop the thread from generating further notifications
mCondition.open();
}
private Runnable mTask = new Runnable() {
public void run() {
for (int i = 0; i < 4; ++i) {
showNotification(R.drawable.stat_happy,
R.string.status_bar_notifications_happy_message);
if (mCondition.block(5 * 1000))
break;
showNotification(R.drawable.stat_neutral,
R.string.status_bar_notifications_ok_message);
if (mCondition.block(5 * 1000))
break;
showNotification(R.drawable.stat_sad,
R.string.status_bar_notifications_sad_message);
if (mCondition.block(5 * 1000))
break;
}
// Done with our work... stop the service!
NotifyingService.this.stopSelf();
}
};
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private void showNotification(int moodId, int textId) {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(textId);
// Set the icon, scrolling text and timestamp.
// Note that in this example, we pass null for tickerText. We update the icon enough that
// it is distracting to show the ticker text every time it changes. We strongly suggest
// that you do this as well. (Think of of the "New hardware found" or "Network connection
// changed" messages that always pop up)
Notification notification = new Notification(moodId, null, System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, NotifyWords.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.status_bar_notifications_mood_title),
text, contentIntent);
// Send the notification.
// We use a layout id because it is a unique number. We use it later to cancel.
mNM.notify(MOOD_NOTIFICATIONS, notification);
}
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new Binder() {
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
return super.onTransact(code, data, reply, flags);
}
};
private NotificationManager mNM;
}