發送通知android
public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); String serviceContextName = Context.NOTIFICATION_SERVICE; NotificationManager notificationManager; notificationManager = (NotificationManager) getSystemService(serviceContextName); //設置圖標,標題,時間 int icon = R.drawable.ic_launcher; String tickerText = "Notification"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); notification.number++; //設置展開通知的內容 Context context = getApplicationContext(); String expandedText = "Extended status text"; String expandedTitle = "Notification Title"; //當單擊展開的文本時,用於啓動一個活動的意圖 Intent intent = new Intent(this, MyActivity.class); PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0); notification.setLatestEventInfo(context, expandedText, expandedText, launchIntent); //若要定製佈局(若手動設置contentView時,必須同時設置contentIntent) notification.contentView = new RemoteViews(this.getPackageName(), R.layout.my_status_window_layout); notification.contentIntent = launchIntent; //修改通知中的視圖 notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_launcher);//將後者搬到前者這個id的視圖上 notification.contentView.setTextViewText(R.id.status_text, "Current Progress"); notification.contentView.setProgressBar(R.id.status_progress, 100, 50, false); //觸發通知 int notificationRef = 1; notificationManager.notify(notificationRef, notification); //消除通知 notificationManager.cancel(notificationRef); notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE; //設置上默認的聲音和震動 //自定義通知聲音 Uri ringURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); notification.sound = ringURI; //自定義通知震動,需添加一個permission <uses-permission android:name="android.permission.VIBRATE" /> long[] vibrate = new long[] {1000,1000,1000,1000,1000};//一個值震動多久,下一個值暫定多久,這裏持續了5秒 notification.vibrate = vibrate; //自定義通知閃屏 notification.ledARGB = Color.RED; notification.ledOffMS = 0;//設置頻率 notification.ledOnMS = 1; //若OnMS也是0,則將燈關閉 notification.flags = notification.flags | Notification.FLAG_SHOW_LIGHTS; //持續的通知 notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;//正在進行的事件 //連續的通知 notification.flags = notification.flags | Notification.FLAG_INSISTENT;//一直重複音頻震動閃爍直到被取消 } }
警報ide
public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //警報。警報類型: // RTC_WAKEUP指定時間喚醒並觸發意圖; // RTC指定時間觸發意圖但不喚醒設備; // ELAPSED_REALTIME設備啓動後通過的時間觸發待處理的意圖,但不喚醒設備; // ELAPSED_REALTIME_WAKEUP設備啓動並通過指定時間後喚醒設備並觸發意圖 AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE); int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP; long timeOrLengthofWait = 10000; String ALARM_ACTION = "ALARM_ACTION"; Intent intentToFire = new Intent(ALARM_ACTION); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0); alarms.set(alarmType, timeOrLengthofWait, pendingIntent); //取消報警 alarms.cancel(pendingIntent); //重複報警 //若是已經喚醒,那麼每小時觸發一次意圖 alarms.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 60*60*1000, 60*60*1000, pendingIntent); //每小時喚醒並觸發一個警報 alarms.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 60*60*1000, AlarmManager.INTERVAL_DAY, pendingIntent); } }