Android and Notifications (NOTIFICATION_SERVICE) |
Android and Notifications (NOTIFICATION_SERVICE) ใน OS ของ Android ในขณะที่เราใช้งาน Application ต่าง ๆ ที่ทำงานอยู่นั้น บางครั้งจะมี Notification ทีทำงานอยู่ในระดับ OS ที่สามารถแสดงข้อความ Notification แจ้งให้ผู้ใช้งานทราบว่ามีข้อความใหม่ ๆ เข้ามาในขณะนั้น โดยข้อความเตือนจะปรากฏที่ Title Bar เล็ก ๆ บนสุดของ Application โดยจะแจ้งเตือนประมาณ 1-3 วินาที
ถ้าเคยใช้ Android หรือ iOS อยู่แล้วก็จะคุ้นกับฟีเจอร์นี้ดี โดย Notification ตัวนี้เราสามารถทำการดูรายการ Notification อื่น ๆ ด้วยการ Slide หน้าจอลงมาข้างล่าง และโปรแกรมก็จะแสดง Notification ทั้งหมดที่มีอยู่ในปัจจุบัน และเราสามารถคลิกไปยัง Notification นั้น ๆ เพื่อดูรายละเอียด หรือเข้าไปในส่วนต่าง ๆ ที่โปรแกรมได้ออกแบบไว้
Example ในตัวอย่างนี้เราจะมาทดสอบการสร้าง Notification เพื่อแจ้งไปยังหน้าจอของผู้ใช้ และเมื่อผู้ใช้ได้คลิกที่ Notification ที่ได้สร้างขึ้นมาก็จะทำการ Intent ไปยัง Activity ที่กำหนด โดยใน Activity ดังกล่าวจะเป็นรายละเอียดที่ได้แจ้งผ่านไปยัง Notification

โครงสร้างของไฟล์ประกอบด้วย 4 ไฟล์คือ MainActivity.java, activity_main.xml และ ReceiverActivity.java, activity_receiver.xml

ออกแบบหน้าจอ XML Layout ดังรูป
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Test Create" />
</RelativeLayout>

activity_receiver.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
android:text="This is the your braking time."
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="34dp"
android:text="This is the your braking time."
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="36dp"
android:text="This is the your braking time."
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
ReceiverActivity.java
package com.myapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class ReceiverActivity extends Activity {
int num = 0,notificationID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiver);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MainActivity.java
package com.myapp;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnCreate = (Button) findViewById(R.id.btnCreate);
btnCreate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
createNotification(v);
}
});
}
public void createNotification(View view) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(android.R.drawable.btn_star_big_on,
"New notification", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
String Title = "New mesage!";
String Message = "This is the your braking time.";
Intent intent = new Intent(this, ReceiverActivity.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, Title, Message, activity);
notification.number += 1;
notification.defaults = Notification.DEFAULT_SOUND; // Sound
notification.defaults = Notification.DEFAULT_VIBRATE; // Vibrate
notificationManager.notify(1, notification);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
AndroidManifest.xml
<activity
android:name="ReceiverActivity"
android:theme="@style/AppTheme"
android:screenOrientation="portrait"
android:label="@string/title_activity_main" />
เพิ่มคำสั่งของ Activity ReceiverActivity ลงในไฟล์ AndroidManifest.xml ด้วย
คำอธิบาย
String Title = "New mesage!";
String Message = "This is the your braking time.";
จาก Code จะเห็นว่ามีการสร้าง Notification มี Title ว่า New mesage! และ Message ว่า This is the your braking time.
notification.defaults = Notification.DEFAULT_SOUND; // Sound
notification.defaults = Notification.DEFAULT_VIBRATE; // Vibrate
// DEFAULT_SOUND คือกำหนดเสียงและ DEFAULT_VIBRATE กำหนดให้สั่น
Screenshot

ทดสอบการสร้าง Notifications โดยคลิกที่ปุ่ม Test Create (แต่ในกรณีที่ใช้งานจริง ๆ สามารถสร้าง Notifications ภายใต้ Background Process หรือจากส่วนอื่น ๆ ของโปรแกรมที่ทำงานอยู่)

สังเกตุว่า Title Bar ของ Android จะมี Notification แจ้งให้ทราบ

Slide หน้าจอเพื่อดูรายการ Notifications ทั้งหมด

แสดง Notifications ให้เข้าใจว่า Notifications ตัวนี้จะเป็นการทำงานในระดับ OS ซึ่งนั่นหมายความว่า Application อื่น ๆ ถ้ามีการสร้าง Notifications ก็จะมาแสดงอยู่ใน List รายการนี้เช่นเดียวกัน

กรณีที่คลิกที่ Notifications ที่เราได้สร้างไว้ และในโปรแกรมได้มีการ Intent ไปยัง Activity ของ ReceiverActivity
.
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
  |
|
|
Create/Update Date : |
2012-07-03 17:15:19 /
2017-03-26 22:16:17 |
|
Download : |
No files |
|
Sponsored Links / Related |
|
|
|
|
|
|
|