package com.example.android_notification;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.service.notification.NotificationListenerService;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.InboxStyle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
/**
* 通知 Notification 要求 Android 16
* @author Administrator
*
*/
public class MainActivity extends Activity {
private Button button;
private Button button2;
private Button button3;
private Button button4;
private Notification.Builder builder;
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)this.findViewById(R.id.button1);
button2=(Button)this.findViewById(R.id.button2);
button3=(Button)this.findViewById(R.id.button3);
button4=(Button)this.findViewById(R.id.button4);
//获得系统的通知服务
manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
builder=new Notification.Builder(this);
//设置通知的属性,标题 图标 content 等
builder.setTicker("新短信通知");
builder.setContentTitle("会议");
builder.setContentText("明天八点半开会,请准时参加");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pendingIntent);
manager.notify(100, builder.build());
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker("Ticker");
builder.setContentTitle("有新闻通知");
//Set the text (second row) of the notification, in a standard notification.
builder.setContentText("中央新闻");
NotificationCompat.InboxStyle inboxStyle=new NotificationCompat.InboxStyle();
//setStyle(NotificationCompat.Style style)
//Add a rich notification style to be applied at build time.
builder.setStyle(inboxStyle);
//Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel.
builder.setAutoCancel(true);
//Set the third line of text in the platform notification template.
builder.setSubText("subText");
//测试
//Set the argb value that you would like the LED on the device to blnk, as well as the rate.
builder.setLights(10, 100, 10);
//Set the large number at the right-hand side of the notification.
builder.setNumber(10001);
String[] events={"abc","abc2","abc3","abc4","abc5","abc6"};
inboxStyle.setBigContentTitle("Event tracker datil:");
for(int i=0;i<events.length;i++)
{
inboxStyle.addLine(events[i]);
}
Intent intent=new Intent(MainActivity.this,MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,1, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pendingIntent);
manager.notify(1001, builder.build());
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// manager.cancelAll();
manager.cancel(100);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this);
//Notification notification=new Notification();
builder.setContentTitle("通知");
builder.setContentText("第三个进度条");
builder.setTicker("Ticker ProgressBar");
builder.setSmallIcon(R.drawable.ic_launcher);
//设置动作
Intent intent=new Intent(MainActivity.this, MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pendingIntent);
//开启一个线程
new Thread(new Runnable() {
@Override
public void run() {
for(int i=1; i<=100; i++)
{
builder.setProgress(100, i, false);
manager.notify(102, builder.build());
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
builder.setContentText("DownLoad is compeleted");
//builder.setAutoCancel(true);
// manager.notify(102, builder.build());
}
}).start();;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}