Android——狀態列通知欄Notification

來源:互聯網
上載者:User

標籤:

1、AndroidManifest.xml注意要同時註冊Notification02Activity

<!-- 狀態通知欄 Notification -->
        <activity
            android:name="com.example.notification.Notification01Activity"
            android:label="狀態通知" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.notification.Notification02Activity"
            android:label="Notification01Activity" >
        </activity>

2、Notification01Activity.java

public class Notification01Activity extends Activity {
    Button button01, button02, button03, button04;
    // 聲明通知(訊息)管理器
    NotificationManager mNotificationManager;
    Intent mIntent;
    PendingIntent mPendingIntent;
    // 聲明Notification對象
    Notification mNotification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification01);
        // 獲得四個按鈕對象
        button01 = (Button) findViewById(R.id.btn1_notifi);
        button02 = (Button) findViewById(R.id.btn2_notifi);
        button03 = (Button) findViewById(R.id.btn3_notifi);
        button04 = (Button) findViewById(R.id.btn4_notifi);
        // 初始化NotificationManager對象*************
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // 點擊通知時轉移內容*********查看通知的具體內容
        mIntent = new Intent(Notification01Activity.this,
                Notification02Activity.class);
        // 主要是設定點擊通知時顯示內容的類***********
        mPendingIntent = PendingIntent.getActivity(Notification01Activity.this,
                0, mIntent, 0);
        // 構造Notification對象
        mNotification = new Notification();
        button01.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                /* 狀態列上的通知表徵圖及內容 */
                // 設定通知在狀態列顯示的表徵圖
                mNotification.icon = R.drawable.button1;
                // 當我們點擊通知時顯示的內容
                mNotification.tickerText = "Button01通知內容……";
                // 通知時發出預設聲音
                mNotification.defaults = Notification.DEFAULT_SOUND;
                /* ==============展開狀態列的快訊=========== */
                // 設定通知顯示的參數
                mNotification.setLatestEventInfo(Notification01Activity.this,
                        "Button01", "Button01通知", mPendingIntent);
                // 可理解為執行這個通知
                mNotificationManager.notify(0, mNotification);
            }
        });
        button02.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                mNotification.icon = R.drawable.button2;
                mNotification.tickerText = "Button02通知內容……";
                mNotification.defaults = Notification.DEFAULT_SOUND;
                mNotification.setLatestEventInfo(Notification01Activity.this,
                        "Button02", "Button02通知", mPendingIntent);
                mNotificationManager.notify(0, mNotification);
            }
        });
        button03.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                mNotification.icon = R.drawable.button3;
                mNotification.defaults = Notification.DEFAULT_SOUND;
                mNotification.tickerText = "Button03通知內容……";
                mNotification.setLatestEventInfo(Notification01Activity.this,
                        "Button03", "Button03通知", mPendingIntent);
                mNotificationManager.notify(0, mNotification);
            }
        });
        button04.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                mNotification.icon = R.drawable.button31;
                mNotification.defaults = Notification.DEFAULT_SOUND;
                mNotification.tickerText = "Button04通知內容……";
                mNotification.setLatestEventInfo(Notification01Activity.this,
                        "Button04", "Button04通知", mPendingIntent);
                mNotificationManager.notify(0, mNotification);
            }
        });
    }

}

3、Notification02Activity.java

public class Notification02Activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //這裡直接限制一個TextView
        setContentView(R.layout.activity_notification02);
    }
}

4、activity_notification02.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="狀態列提示示範協助實現介面!" />

</LinearLayout>

5、activity_notification01.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/btn1_notifi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button01" />

    <Button
        android:id="@+id/btn2_notifi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button02" />

    <Button
        android:id="@+id/btn3_notifi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button03" />

    <Button
        android:id="@+id/btn4_notifi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button04" />

</LinearLayout>

Android——狀態列通知欄Notification

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.