Вы находитесь на странице: 1из 24

Lecture 9

LECTURE 9 NOTIFICATIONS , DIALOGS AND MEDIA

Notifications

Notifications

Status Notification
adds an icon with a message to the system's status bar and a notification message in the notifications window. When the user selects the notification, Android fires an Intent that is defined by the Notification You can also configure the notification to alert the user with a sound, a vibration, and flashing lights on the device.

Hint : Service and Notification


A background service should never launch an activity on its own in order to receive user interaction. The service should instead create a status notification that will launch the activity when selected by the user.

Code Sample
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(icon, tickerText, when); Intent notificationIntent = new Intent(this, MyAlarmService.class); PendingIntent contentIntent = PendingIntent.getService(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent); mNotificationManager.notify(HELLO_ID, notification);

Setting Sound for the notification


notification.sound = Uri.parse(file///sdcard/myfile.mp3);

or
notification.sound = Uri.parse("android.resource://" + getPackageName() +"/"+ R.raw.mafile);

More about notifications


http://developer.android.com/guide/topics/ui/notifiers/notifications.htm

Dialogs

Dialogs
A dialog is usually a small window that appears in front of the current Activity. The underlying Activity loses focus and the dialog accepts all user interaction.

AlertDialog
ProgressDialog DatePickerDialog TimePickerDialog

Showing Dialogs
Override onCreateDialog(int id) method to initialize and create dialogs Use showDialog(int id) to show specific dialog

Alert Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure you want to exit?");

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()


{ public void onClick(DialogInterface dialog, int id) { } }); AlertDialog alert = builder.create(); alert.show();

Alert Dialog : List


AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Pick a color"); builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {

Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();

}
}); AlertDialog alert = builder.create(); alert.show();

Showing Options
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener()

{
public void onClick(DialogInterface dialog, int item) { Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); } });

Progress Dialog

See More About Dialogs


http://developer.android.com/guide/topics/ui/dialogs.html

Media

Playing Media File

Playing a Video
VideoView ve = (VideoView)findViewById(R.layout.vidview); ve.setMediaController(new MediaController(getApplicationContext()));

ve.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+ R.raw.vid));


ve.start(); ve.requestFocus();

Using The Camera


Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Animation
Frame By frame Animation Tweens Animation

Scale
Rotate

Frame By Frame

Tween Animation
<set android:interpolator="@android:anim/accelerate_interpolator"> <scale android:fromXScale="1.4 android:toXScale="0.0" android:fromYScale="0.6" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="400 android:startOffset="700"/> </set>

See More about Animations


http://developer.android.com/guide/topics/resources/animation-resource.html

Вам также может понравиться