Xem mẫu

  1. 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: Hello World, NotificationExample! NotificationExample Notification Example, press the button Notification to add a notification. Show Notification This is the activity started when you press the notification. Tiếp theo các bạn chỉnh sửa file main.xml:
  2. 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"> Các bạn tạo thêm file customlayout có nội dung:
  3. 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"> Tiếp theo các bạn tạo file notification_viewer.xml có nội dung: 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
  4. 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: 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; Lập trình Android – http://laptrinhdidong.vn Page 4
  5. Trung tâm Tin học – ĐH KHTN import android.view.View; import android.widget.Button; 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)).setOnClickListene r(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(); Lập trình Android – http://laptrinhdidong.vn Page 5
  6. Trung tâm Tin học – ĐH KHTN break; } } } }; } 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
nguon tai.lieu . vn