android實現通知欄消息


一、原理 
消息推送有兩種,一種是客戶端定時直接到服務器搜索消息,如果發現有新的消息,就獲取消息下來;另一種是服務器向客戶端發送消息,也就是當有信息消息時,服務器端就會向客戶端發送消息。

二、步驟(代碼)

注: Notification //是具體狀態欄對象,設置Icon、文字、聲音等。
NotificationMangager //狀態欄通知管理類、負責發消息、清理消息。


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.provider.MediaStore.Audio;
import android.util.Log;
import android.widget.RemoteViews;

/**
* 消息推送

* @author Msquirrel

*/
public class MessageService extends Service {

private String TAG = "-----------";

private MessageThread messageThread = null;

// 點擊查看
private Intent messageIntent = null;
private PendingIntent messagePendingIntent = null;

// 通知欄消息
private int messageNotificationID = 1000;
private Notification messageNotification = null; // 是具體的狀態欄通知對象,可以設置icon、文字、提示聲音、振動等等參數。
private NotificationManager messageNotificatioManager = null; // 是狀態欄通知的管理類,負責發通知、清楚通知等。 
private RemoteViews contentView = null;

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 初始化
// messageNotification = new
// Notification(R.drawable.icon,"新消息11",System.currentTimeMillis());//*簡單消息版本里的*此版本不使用
messageNotification = new Notification();
messageNotification.icon = R.drawable.icon;// 狀態欄提示圖標
messageNotification.tickerText = "嘿嘿,測試消息推送";// 狀態欄提示消息

contentView = new RemoteViews(getPackageName(), R.layout.view);// 消息內容容器
contentView.setImageViewResource(R.id.image, R.drawable.icon);// 消息容器界面的圖標

messageNotification.contentView = contentView;// 把消息容器和消息綁定

// messageNotification.icon = R.drawable.icon;//*簡單消息版本里的*此版本不使用
// messageNotification.tickerText = "新消息11";//*簡單消息版本里的*此版本不使用
// messageNotification.when=System.currentTimeMillis();
// //*簡單消息版本里的*此版本不使用

// messageNotification.defaults |= Notification.DEFAULT_SOUND;//聲音
// messageNotification.defaults |= Notification.DEFAULT_LIGHTS;//燈
// messageNotification.defaults |= Notification.DEFAULT_VIBRATE;//震動

// messageNotification.sound = Uri.parse("file:///sdcard/to.mp3");
messageNotification.sound = Uri.withAppendedPath(
Audio.Media.INTERNAL_CONTENT_URI, "2");// 選音樂清單的第2首歌做消息聲音
// messageNotification.ledARGB = 0xff00ff00;//燈的顏色
// messageNotification.ledOnMS = 300; //亮的時間
// messageNotification.ledOffMS = 1000; //滅的時間
// messageNotification.flags |= Notification.FLAG_SHOW_LIGHTS;//顯示燈

// long v[]= {0,100,200,300}; //震動頻率
// messageNotification.vibrate = v;
//

messageNotification.flags |= Notification.FLAG_AUTO_CANCEL;// 點擊消息后,該消息自動退出
messageNotification.flags |= Notification.FLAG_ONGOING_EVENT;// 在上方運行消息欄中出現
// messageNotification.flags|=Notification.FLAG_NO_CLEAR;//此消息不會被清除

messageNotificatioManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
messageIntent = new Intent(this, ShowMessage .class);// 點擊消息后,要跳轉的界面 ( 對應 詳細消息的界面 )

// 開啟線程
messageThread = new MessageThread();// 該線程每10秒,發布一條消息出來
messageThread.isRunning = true;// 設置為false后,線程跳出循環並結束對
messageThread.start();
Log.i(TAG, "startCommand");
return super.onStartCommand(intent, flags, startId);
}

/**
* 從服務器端獲取消息
*/
class MessageThread extends Thread {
// 設置為false后,線程跳出循環並結束
public boolean isRunning = true;

public void run() {
while (isRunning) {
try {

String serverMessage = getServerMessage();

if (serverMessage != null && !"".equals(serverMessage)) {
// 更新通知欄
// messageNotification.setLatestEventInfo(MessageService.this,"新消息","哇~有 新消息耶!"+serverMessage,messagePendingIntent);//*簡單消息版本里的*此版本不使用

contentView.setTextViewText(R.id.text, serverMessage);// 設置消息內容

messageIntent.putExtra("message", serverMessage);// 為意圖添加參數
messagePendingIntent = PendingIntent.getActivity(
MessageService.this, 0, messageIntent,
PendingIntent.FLAG_CANCEL_CURRENT);// 將意圖裝入 延遲意圖
messageNotification.contentIntent = messagePendingIntent;// 將延遲意圖裝入消息
messageNotificatioManager.notify(messageNotificationID,
messageNotification);// 啟動Notification

Log.i(TAG, "發出消息");

// messageNotificatioManager.cancel(messageNotificationID-1);//新消息來后,消除之前的一條消息(只顯示最新消息)
// 配置好下條消息的id號
messageNotificationID++;
}
// 休息10秒鍾
Thread.sleep(10000);
// 獲取服務器消息
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

/**
* 模仿服務器發送過來的消息,僅作示例

* @return 返回服務器要推送的消息,否則如果為空的話,不推送
*/
public String getServerMessage() {
Log.i(TAG, "getmessage");
return "親, 測試成功啦~~!";

}

@Override
public void onDestroy() {
// System.exit(0);

messageThread.isRunning = false;
// 或者,二選一,推薦使用System.exit(0),這樣進程退出的更干凈
// messageThread.isRunning = false;
super.onDestroy();
Log.i(TAG, "destroy");
}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM