【android】Notification的使用

來源:互聯網
上載者:User

 

package com.noti.activity;

 

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RemoteViews;

 

public class Main extends Activity {

    /** Called when the activity is first created. */

private Button btn;//使用系統的notification布局

private Button btn1;//使用自訂布局

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        btn = (Button) findViewById(R.id.btn);

        btn1 = (Button) findViewById(R.id.btn1);

        btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

showNotification();

}

});

     btn1.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

showMyNotification();

}

});

    }

    public void showNotification(){

  獲得系統的NotificationManager對象

    NotificationManager  notificationManager = (NotificationManager)       getSystemService(Context.NOTIFICATION_SERVICE);

 //執行個體化一個的notification

Notification notification = new Notification(R.drawable.image1, "notice", System.currentTimeMillis());

Intent  intent =  new Intent(Main.this,OtherActivity.class);//點擊這個notification觸發的Intent

PendingIntent pendingIntent =  PendingIntent.getActivity(this, 0, intent, 1);

notification.setLatestEventInfo(this, "title","a message", pendingIntent);//顯示notification的布局和觸發器

notificationManager.notify(R.id.btn, notification);//發出通知,根據Id判斷是否為新的通知

    }

 

    public void showMyNotification(){

    NotificationManager  notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.icon, "notice", System.currentTimeMillis());

//要實現自訂布局,首先自己定義一個 布局檔案notification.xml,如放一個ImageView 和 TextView,用這個布局檔案產生 //RemoteViews的執行個體

RemoteViews contentview = new RemoteViews(getPackageName(), R.layout.notification);

contentview.setImageViewResource(R.id.image, R.drawable.image1);

contentview.setTextViewText(R.id.text, "hello world");

//將視圖賦值給notification

notification.contentView = contentview;

notification.contentIntent = pendingIntent;

//以上代碼代替系統的setLatestEventInfo()方法

Intent  intent =  new Intent(Main.this,OtherActivity.class);

PendingIntent pendingIntent =  PendingIntent.getActivity(this, 0, intent, 1);

 

notificationManager.notify(R.id.btn, 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.