範例35:狀態列與訊息提醒,設定振動
package ex36.Notification_2;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
public class Notification_2 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);
// 使用振動
note.vibrate = new long[]{100, 250, 100, 500};
// 使用LED
note.ledARGB = Color.RED;
note.flags |= Notification.FLAG_SHOW_LIGHTS;
note.ledOnMS = 200;
note.ledOffMS = 300;
notif.notify(NOTIF_ID, note); // 送出提醒訊息
}
}
此方法需要設定權限"需先在AndroidManifest.xml允許存取android.permission.VIBRATE這個權限"
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
public class Notification_2 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);
// 使用振動
note.vibrate = new long[]{100, 250, 100, 500};
// 使用LED
note.ledARGB = Color.RED;
note.flags |= Notification.FLAG_SHOW_LIGHTS;
note.ledOnMS = 200;
note.ledOffMS = 300;
notif.notify(NOTIF_ID, note); // 送出提醒訊息
}
}
此方法需要設定權限"需先在AndroidManifest.xml允許存取android.permission.VIBRATE這個權限"
留言
張貼留言