狀態列與訊息提醒notification manager
第一頁
package ex35.Notification_1;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class Notification_1 extends Activity{
private static final int NOTIF_ID = 1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// Button元件的事件處理 - 顯示提醒訊息
@SuppressWarnings("deprecation")
public void btn1_Click(View view)
{
// 取得NotificationManager系統服務
NotificationManager notif = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 建立狀態列顯示的提醒訊息
Notification note = new Notification(R.drawable.icon,"新郵件", System.currentTimeMillis());
Intent intent = new Intent(this, NotificationActivity.class);
intent.putExtra("NOTIFICATION_ID", NOTIF_ID);
// 建立PendingIntent物件
PendingIntent pI = PendingIntent.getActivity(this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 建立通知的詳細內容
note.setLatestEventInfo(this, "新郵件", "你有未讀郵件", pI);
notif.notify(NOTIF_ID, note); // 送出提醒訊息
}
}
第二頁
package ex35.Notification_1;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
public class NotificationActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
NotificationManager notif = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
// 取消狀態列的提醒訊息
notif.cancel(getIntent().getExtras().getInt("NOTIFICATION_ID"));
}
}
package ex35.Notification_1;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class Notification_1 extends Activity{
private static final int NOTIF_ID = 1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// Button元件的事件處理 - 顯示提醒訊息
@SuppressWarnings("deprecation")
public void btn1_Click(View view)
{
// 取得NotificationManager系統服務
NotificationManager notif = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 建立狀態列顯示的提醒訊息
Notification note = new Notification(R.drawable.icon,"新郵件", System.currentTimeMillis());
Intent intent = new Intent(this, NotificationActivity.class);
intent.putExtra("NOTIFICATION_ID", NOTIF_ID);
// 建立PendingIntent物件
PendingIntent pI = PendingIntent.getActivity(this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 建立通知的詳細內容
note.setLatestEventInfo(this, "新郵件", "你有未讀郵件", pI);
notif.notify(NOTIF_ID, note); // 送出提醒訊息
}
}
第二頁
package ex35.Notification_1;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
public class NotificationActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
NotificationManager notif = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
// 取消狀態列的提醒訊息
notif.cancel(getIntent().getExtras().getInt("NOTIFICATION_ID"));
}
}
留言
張貼留言