package com.example.notification;
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.os.Handler;
public class MainActivity extends Activity
{
private Handler handler = new Handler();
private int mId = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler.post(runnable);
}
private Runnable runnable = new Runnable()
{
@Override
public void run()
{
if(mId < 3)
{
sendNotification(mId);
mId++;
}
handler.postDelayed(runnable, 5000);
}
};
/**
* 根据不同的ID发送不同类型的通知
*
* @param mId
*/
@SuppressWarnings("deprecation")
protected void sendNotification(int mId)
{
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new Notification(R.drawable.ic_launcher, "状态栏显示滚轮信息", System.currentTimeMillis());
noti.defaults |= Notification.DEFAULT_VIBRATE;
// noti.vibrate = null;
noti.defaults |= Notification.DEFAULT_SOUND;
// noti.sound = null;
noti.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("fromTag", "传递数据");
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
noti.setLatestEventInfo(MainActivity.this, "通知栏标题" + mId, "通知栏信息详情" + mId, pendingIntent);
notificationManager.notify(mId, noti);
}
@Override
protected void onDestroy()
{
handler.removeCallbacks(runnable);
super.onDestroy();
}
}
Android Notification
最新推荐文章于 2021-02-20 15:15:35 发布