1 创建NotificationMananger 对象,负责发出和取消通知。
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
2 初始化Notification设置图片资源,显示的文本、时间、能否取消
Notification n =
new
Notification(R.drawable.chat,
"Hello,there!"
, System.currentTimeMillis());
3 展示通知
nm.notify(0,n);
4 取消通知
nm.cancelNotify(0,n);
public class MainActivity extends Activity implements OnClickListener {
private Notification notification;
private NotificationManager nManager;
public static int NOTIFICATION = 112;
private int progress =0;
private boolean running = true;
private Button bt_showNof,bt_cancelNof;
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 1:
cancelNotification();
Toast.makeText(MainActivity.this, "通知消失", Toast.LENGTH_SHORT).show();
break;
case 2:
Log.e("test","测试数据:"+ progress);
notification.contentView.setProgressBar(R.id.ProgressBar01, 100, progress, false);
nManager.notify(NOTIFICATION, notification);
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initNotification();
bt_showNof = (Button) findViewById(R.id.bt_show);
bt_cancelNof = (Button) findViewById(R.id.bt_cancel);
bt_showNof.setOnClickListener(this);
bt_cancelNof.setOnClickListener(this);
}
@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;
}
private void initNotification(){
nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.ic_launcher, "正在同步...", System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.contentView = new RemoteViews(this.getPackageName(), R.layout.notification);
notification.contentView.setProgressBar(R.id.ProgressBar01, 100, 0, false);
notification.contentIntent = PendingIntent.getActivity(this, 0, new Intent(this ,MainActivity.class), 0);
}
private void showNotification(){
running = true;
nManager.notify(NOTIFICATION, notification);
}
private void cancelNotification(){
if(nManager != null){
nManager.cancel(NOTIFICATION);
}
progress = 0;
running = false;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt_show:
showNotification();
new MyThread().start();
break;
case R.id.bt_cancel:
cancelNotification();
default:
break;
}
}
class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while (running) {
progress = progress+10;
if(progress >= 110){
mHandler.sendEmptyMessage(1);
}else{
mHandler.sendEmptyMessage(2);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
private Notification notification;
private NotificationManager nManager;
public static int NOTIFICATION = 112;
private int progress =0;
private boolean running = true;
private Button bt_showNof,bt_cancelNof;
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 1:
cancelNotification();
Toast.makeText(MainActivity.this, "通知消失", Toast.LENGTH_SHORT).show();
break;
case 2:
Log.e("test","测试数据:"+ progress);
notification.contentView.setProgressBar(R.id.ProgressBar01, 100, progress, false);
nManager.notify(NOTIFICATION, notification);
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initNotification();
bt_showNof = (Button) findViewById(R.id.bt_show);
bt_cancelNof = (Button) findViewById(R.id.bt_cancel);
bt_showNof.setOnClickListener(this);
bt_cancelNof.setOnClickListener(this);
}
@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;
}
private void initNotification(){
nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.ic_launcher, "正在同步...", System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.contentView = new RemoteViews(this.getPackageName(), R.layout.notification);
notification.contentView.setProgressBar(R.id.ProgressBar01, 100, 0, false);
notification.contentIntent = PendingIntent.getActivity(this, 0, new Intent(this ,MainActivity.class), 0);
}
private void showNotification(){
running = true;
nManager.notify(NOTIFICATION, notification);
}
private void cancelNotification(){
if(nManager != null){
nManager.cancel(NOTIFICATION);
}
progress = 0;
running = false;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt_show:
showNotification();
new MyThread().start();
break;
case R.id.bt_cancel:
cancelNotification();
default:
break;
}
}
class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while (running) {
progress = progress+10;
if(progress >= 110){
mHandler.sendEmptyMessage(1);
}else{
mHandler.sendEmptyMessage(2);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}