Android編程自訂Notification執行個體分析_Android

來源:互聯網
上載者:User

本文執行個體講述了Android編程自訂Notification的用法。分享給大家供大家參考,具體如下:

Notification是一種讓你的應用程式在不使用Activity的情況下警示使用者,Notification是看不見的程式組件警示使用者有需要注意的事件發生的最好途徑。

作為UI部分,Notification對行動裝置來說是最適合不過的了。使用者可能隨時都帶著手機在身邊。一般來說,使用者會在後台開啟幾個程式,但不會注意它們。在這樣的情形下,當發生需要注意的事件時,能夠通知使用者是很重要的。

Notification由NotificationManger統一管理,目前包含的能力有:

❑建立一個狀態條表徵圖。

❑在擴充的狀態條視窗中顯示額外的資訊(和啟動一個Intent)。

❑閃燈或LED。

❑電話震動。

❑發出聽得見的警告聲(鈴聲,儲存的音效檔)。

自訂Notification效果圖:

 

自訂的布局檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_rv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="haha" /><ProgressBar style="@android:style/Widget.ProgressBar.Horizontal" android:id="@+id/pb_rv" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>

建立Notification:

public class CustomNotificationActivity extends Activity {  NotificationManager notificationManager;  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    //擷取到系統的notificationManager    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  }  public void click(View view ){    //執行個體化一個notification     String tickerText = "IP號碼 設定完畢";     long when = System.currentTimeMillis();     Notification notification = new Notification(R.drawable.icon, tickerText, when);     //不能手動清理     //notification.flags= Notification.FLAG_NO_CLEAR;     //添加音樂     //notification.sound = Uri.parse("/sdcard/haha.mp3");     //設定使用者點擊notification的動作     // pendingIntent 延期的意圖     Intent intent = new Intent(this,Bactivity.class);     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);     notification.contentIntent = pendingIntent;     //自訂介面     RemoteViews rv = new RemoteViews(getPackageName(), R.layout.noti_layout);     rv.setTextViewText(R.id.tv_rv, "我是自訂的 notification");     rv.setProgressBar(R.id.pb_rv, 80, 20, false);     notification.contentView = rv;     //把定義的notification 傳遞給 notificationmanager     notificationManager.notify(0, notification);  }}

希望本文所述對大家Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.