通知欄

<main.xml>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button 
        android:id="@+id/bn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start"
        android:textSize="12sp"/>

    <Button 
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/stop"
        android:textSize="12sp"/>

</LinearLayout>

<MainActivity.java>


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.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
 static final int NOTIFICATION_ID=0x1123;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  //獲取應用界面中的Button對象
  Button bn=(Button) findViewById(R.id.bn);
  //爲按鈕的單擊事件綁定事件監聽器
  bn.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
    //創建一個啓動其他Activity的Intent
    Intent intent=new Intent(MainActivity.this,
      QtActivity.class);
    PendingIntent pi =PendingIntent.getActivity(MainActivity.this
      , 0, intent,PendingIntent .FLAG_UPDATE_CURRENT);
    //創建一個Notification
    Notification notify=new Notification();
    //爲Notification設置圖標,該圖標顯示在狀態欄
    notify.icon=R.drawable.ic_launcher;
    //設置文本內容顯示在狀態欄
    notify.tickerText="通知";
    //設置發生時間
    notify.when=System.currentTimeMillis();
    //設置聲音
    notify.defaults=Notification.DEFAULT_SOUND;
    //設置默認聲音 默認震動 默認閃光燈
    notify.defaults=Notification.DEFAULT_ALL;
    //設置事件信息
    notify.setLatestEventInfo(MainActivity.this,
      "普通通知", "點擊查看", pi );
    //獲取系統的NotificationManager服務
    NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    //發送通知
    notificationManager.notify(NOTIFICATION_ID,notify);

   }
  });

  Button button=(Button) findViewById(R.id.button);
  button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {
    //獲取系統的NotificationManager服務
    NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //quxiaotongzhi
    notificationManager.cancel(NOTIFICATION_ID);

   }
  });
 }


}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章