Trung tâm Tin học – ĐH KHTN Tạo “Notification” Sau đây mình sẽ tạo 1 demo nho nhỏ để tạo các Notice (nhắc nhở trên Android): Đầu tiên các bạn tạo 1 project như sau: Project name: NotificationExample Build Target: Android 2.3.3 Application name: NotificationExample Package name: org.example. NotificationExample Create Activity: NotificationExample Sau đó các bạn chỉnh sửa strings.xml như sau: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, NotificationExample!</string> <string name="app_name">NotificationExample</string> <string name="info">Notification Example, press the button Notification to add a notification.</string> <string name="btn_default_notification_text">Show Notification</string> <string name="notification_string">This is the activity started when you press the notification.</string> </resources> Tiếp theo các bạn chỉnh sửa file main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.9"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:text="@string/info"/> </LinearLayout> <LinearLayout android:orientation="horizontal" Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.1" android:gravity="bottom|center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/btn_default_notification" android:text="@string/btn_default_notification_text"/> </LinearLayout> </LinearLayout> Các bạn tạo thêm file customlayout có nội dung: <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <ImageView android:id="@+id/custom_view_image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="0.1" android:src="@drawable/icon" android:scaleType="fitCenter"> </ImageView> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_vertical" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_weight="0.9"> <TextView android:id="@+id/custom_view_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.9" android:gravity="center_vertical" android:text="Sample message" android:textColor="#000"> </TextView> <ProgressBar style="?android:attr/progressBarStyleHorizontal" Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN android:id="@+id/custom_view_progress" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.9" android:max="100" android:gravity="center_vertical"> </ProgressBar> </LinearLayout> </LinearLayout> Tiếp theo các bạn tạo file notification_viewer.xml có nội dung: <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.9"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:text="@string/notification_string"/> </LinearLayout> </LinearLayout> Tiếp theo các bạn them vào package file Constants.java: package org.example.NotificationExample; public interface Constants { public static final int NOTIFICATION_ID = 10001; static final int PROGRESS_MAX = 100; } Và thêm tiếp file NotofocationViewer.java: package org.example.NotificationExample; import android.app.Activity; import android.app.NotificationManager; import android.content.Context; import android.os.Bundle; Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN public class NotificationViewer extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification_viewer); NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(Constants.NOTIFICATION_ID); } } Để sử dụng Activity này các bạn chỉnh sửa file AndroidManifest.xml như sau: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.example.NotificationExample" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".NotificationExample" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NotificationViewer"/> </application> <uses-permission android:name="android.permission.VIBRATE"></uses-permission> </manifest> Cuối cùng các bạn chỉnh sữa file NotificationExample.java như sau: package org.example.NotificationExample; import android.app.Activity; import android.os.Bundle; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.view.View; import android.widget.Button; Lập trình Android – http://laptrinhdidong.vn Page 4 Trung tâm Tin học – ĐH KHTN public class NotificationExample extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ((Button)findViewById(R.id.btn_default_notification)).setOnClickListener(btnClick); } private void addDefaultNotification(){ NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); int icon = R.drawable.icon; CharSequence text = "Notification Text"; CharSequence contentTitle = "Notification Title"; CharSequence contentText = "Sample notification text."; long when = System.currentTimeMillis(); Intent intent = new Intent(this, NotificationViewer.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new Notification(icon,text,when); long[] vibrate = {0,100,200,300}; notification.vibrate = vibrate; notification.ledARGB = Color.RED; notification.ledOffMS = 300; notification.ledOnMS = 300; notification.defaults |= Notification.DEFAULT_LIGHTS; //notification.flags |= Notification.FLAG_SHOW_LIGHTS; notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent); notificationManager.notify(Constants.NOTIFICATION_ID, notification); } private final View.OnClickListener btnClick = new View.OnClickListener() { @Override public void onClick(View v) { switch(v.getId()) { case R.id.btn_default_notification: { addDefaultNotification(); break; } } Lập trình Android – http://laptrinhdidong.vn Page 5 Trung tâm Tin học – ĐH KHTN } }; } Và hình ảnh của ứng dụng sau khi hoàn thành: Các bạn nào muốn đóng góp trao đổi vui lòng post bài vào mục diễn đàn trên trang http://www.laptrinhdidong.vn/ . Lập trình Android – http://laptrinhdidong.vn Page 6 . {0,100,200,300}; notification. vibrate = vibrate; notification. ledARGB = Color.RED; notification. ledOffMS = 300; notification. ledOnMS = 300; notification. defaults |= Notification. DEFAULT_LIGHTS; / /notification. flags. ((Button)findViewById(R.id.btn_default _notification) ).setOnClickListener(btnClick); } private void addDefaultNotification(){ NotificationManager notificationManager = (NotificationManager)getSystemService(Context .NOTIFICATION_ SERVICE); . android:id="@+id/custom_view_image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="0.1" android:src="@drawable/icon" android:scaleType="fitCenter">