android 快速发送通知栏消息测试

为了测试android对于快速发送通知栏消息是否有缓冲机制,我写了下面的示例,可能还不够科学,仅供参考:

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 {
        @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);//点击通知要跳到的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);
        }
    };
}

对应的activity_main.xml文件:




    

你可能感兴趣的:(android 快速发送通知栏消息测试)