安卓學習第33課——notification

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   使用   java   

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:gravity="center_horizontal"    ><Button    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="發送Notification"    android:onClick="send"    /><Button    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="刪除Notification"    android:onClick="del"    />    </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:gravity="center_horizontal"    android:orientation="vertical"    ><!-- 定義一個ImageView --><ImageView    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:src="@drawable/java"    android:layout_gravity="center_horizontal"    /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.notification"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="16"        android:targetSdkVersion="19" />    <!-- 添加操作閃光燈的許可權 -->    <uses-permission android:name="android.permission.FLASHLIGHT" />    <!-- 添加操作震動器的許可權 -->    <uses-permission android:name="android.permission.VIBRATE" />        <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.notification.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name=".OtherActivity"            android:label="@string/other_activity">        </activity>    </application></manifest>

當點擊notification時,實現otherActivity

package com.example.notification;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {    static final int NOTIFICATION_ID=0x123;    NotificationManager nm;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);        }    // 為發送通知的按鈕的點擊事件定義事件處理方法        public void send(View source)        {            // 建立一個啟動其他Activity的Intent            Intent intent = new Intent(MainActivity.this                , OtherActivity.class);            PendingIntent pi = PendingIntent.getActivity(                    MainActivity.this, 0, intent, 0);            Notification notify = new Notification.Builder(this)                // 設定開啟該通知,該通知自動消失                .setAutoCancel(true)                // 設定顯示在狀態列的通知提示資訊                .setTicker("有新訊息")                // 設定通知的表徵圖                .setSmallIcon(R.drawable.notify)                // 設定通知內容的標題                .setContentTitle("一條新通知")                // 設定通知內容                .setContentText("恭喜你,您加薪了,工資增加20%!")                // // 設定使用系統預設的聲音、預設LED燈                // .setDefaults(Notification.DEFAULT_SOUND                // |Notification.DEFAULT_LIGHTS)                // 設定通知的自訂聲音                .setSound(Uri.parse("android.resource://com.example.notification/"                    + R.raw.mylove1))                .setWhen(System.currentTimeMillis())                // 設改通知將要啟動程式的Intent                .setContentIntent(pi).build();            // 發送通知            nm.notify(NOTIFICATION_ID, notify);        }        // 為刪除通知的按鈕的點擊事件定義事件處理方法        public void del(View v)        {            // 取消通知            nm.cancel(NOTIFICATION_ID);        }    }
package com.example.notification;import android.app.Activity;import android.os.Bundle;public class OtherActivity extends Activity{    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        //設定該Activity顯示的頁面        setContentView(R.layout.other);    }}

安卓學習第33課——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.