< CS193A Android Programming

Today a few random topics

Automatic Resource Selection 1 -- drawable

Automatic Resource Selection 2 -- string

Dynamic Pixel Scale Independence

		mMonkey = makeScaled(BitmapFactory.decodeResource(context.getResources(), R.drawable.monkey), 160 * mScale);
		mAlternate = makeScaled(BitmapFactory.decodeResource(context.getResources(), R.drawable.alternate), 160 * mScale);
		// Note: density scaling: here we choose to make the monkey 160 device-independent-pixels wide, exactly 1 inch.
		// By multiplying by the passed in scale, the monkey will be the right number of actual pixels
		// wide to be 1 inch for whatever the display density.

Foreground/Background Hierarchy

Notification Manager

	/** Puts up a notification with the given text.
	 This is not really an appropriate UI for this app, but I wanted
	 to show the code for this case.
	public void makeNotification(CharSequence text) {
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		// Text here appears at the top of the screen at the moment of notification
		int icon = android.R.drawable.ic_dialog_info;
		// Above grabs a built in icon, but could make a custom one in our own R.drawable
		long now = System.currentTimeMillis();
		Notification notification = new Notification(icon, text, now);
		// Make an intent to fire when the user selects the notification .. e.g.
		// intent/activity to show the text message. Here we make a dummy intent
		// that just starts our main activity.
		Intent notificationIntent = new Intent(this, NetworkFetchActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
		// Text here appears in the body of the notification when the user
		// pulls it down to look at it.
		Context context = getApplicationContext();
		CharSequence title = "Notification title";
		CharSequence detailText = text;
		notification.setLatestEventInfo(context, title, detailText, contentIntent);
		// Put up the notification. You can give each notification its own
		// id to support multiple pending ones. Here I just use 1.
		int id = 1;
		mNotificationManager.notify(id, notification);

Last homework! Due Fri Dec 9th, or ask Madiha for an extension. You have a choice of either a "game" project or a "AsyncTask" project.

1. Game: you may start with your "ducks" project or start a new one. Add gesture support. A user drag-gesture on a duck or some other on-screen thing should change its position/trajectory in some way that is up to you. Optional: implement the invalidate(rect) optimization.

2. AsyncTask: your app should have a Start button which starts up an AsyncTask that does something. The AsyncTask should update some status and/or progress bar periodically as it runs. Optional: one interesting application would be that the AsyncTask adds some text to a TextView or list, say, every 10 seconds. Program it to keep running, even after an onPause .. in this way, you can observe when the device goes through a real sleep/wake cycle vs. just blanking the screen.

In case you need it, "block" your AsyncTask to do nothing for a number of milliseconds, use this snippet:

try {
  Thread.sleep(5000); // 5 seconds
catch (InterruptedException ignored)  { }