為了測(cè)試android對(duì)于快速發(fā)送通知欄消息是否有緩沖機(jī)制,我寫了下面的示例,可能還不夠科學(xué),僅供參考:
package com.toby.personal.testlistview;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static int currentSendTimes = 0;
private static final int sendDuration = 2;
private static final int messageWhat = 170228;
private static final int totalSendTimes = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button testBtn = (Button) findViewById(R.id.testBtn);
if (testBtn != null) {
testBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TestTask testTask = new TestTask();
testTask.execute();
}
});
}
}
class TestTask extends AsyncTask<Void, Long, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
currentSendTimes = 0;
waitHandler.sendEmptyMessageDelayed(messageWhat, sendDuration);
return true;
}
}
public void showUpdateNotification(String content) {
Intent updateIntent = new Intent();
updateIntent.setAction("android.intent.action.VIEW");
PendingIntent pt = PendingIntent.getActivity(this, 0, updateIntent, 0);//點(diǎn)擊通知要跳到的activity
NotificationManager notificationManager
= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = getResources();
String title = "有更新版本啦";
int NT_ID = R.string.app_name;
Notification.Builder builder = new Notification.Builder(this);
builder.setContentIntent(pt)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setWhen(System.currentTimeMillis())
.setAutoCancel(false)
.setTicker(title)
.setAutoCancel(false)
.setContentTitle(title)
.setContentText(content);
notificationManager.notify(NT_ID, builder.build());
}
private final Handler waitHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == messageWhat && currentSendTimes < totalSendTimes) {
waitHandler.sendEmptyMessageDelayed(messageWhat, sendDuration);
++currentSendTimes;
showUpdateNotification("CurrentSendTimes: " + currentSendTimes);
}
super.handleMessage(msg);
}
};
}
對(duì)應(yīng)的activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.toby.personal.testlistview.MainActivity">
<Button
android:id="@+id/testBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>