Android 消息通知-Notification

Android 消息通知-Notification,想到这个就想到消息推送,人人,QQ 

推荐阅读:Android 程序错误处理全局处理 http://www.linuxidc.com/Linux/2011-04/34224.htm

  1. import android.app.Activity;  
  2. import android.app.Notification;  
  3. import android.app.NotificationManager;  
  4. import android.app.PendingIntent;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class NotificationTestActivity extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     private Button button01,button02;  
  14.       
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.         button01=(Button) findViewById(R.id.button1);  
  20.         button02=(Button) findViewById(R.id.button2);  
  21.         button01.setOnClickListener(new Mybutton());  
  22.         button02.setOnClickListener(new Mybutton());  
  23.     }  
  24.     class Mybutton implements OnClickListener{  
  25.   
  26.         @Override  
  27.         public void onClick(View v) {  
  28.             // TODO Auto-generated method stub   
  29.             int FLAG=0;  
  30.             NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);//获取系统NotificationManager服务   
  31.             Intent intent=new Intent();  
  32.             intent.setClass(getApplicationContext(), Notification.class);  
  33.             PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(), 0, intent, FLAG);  
  34.             Notification notification=new Notification();  
  35.             notification.when=System.currentTimeMillis();//发出这个通知的时间   
  36.             notification.defaults=Notification.DEFAULT_ALL;//提示方式,有震动,声音,闪关灯   
  37.             switch (v.getId()) {  
  38.             case R.id.button1:  
  39.                 notification.icon=R.drawable.alert_15;  
  40.                 notification.setLatestEventInfo(NotificationTestActivity.this"通知""手机在此联网", pendingIntent);  
  41.                 notification.tickerText="联网通知";  
  42.                 manager.notify(0, notification);  
  43.                 break;  
  44.             case R.id.button2:  
  45.                 manager.cancel(FLAG);//删除当前的notifcation   
  46.                 break;  
  47.             default:  
  48.                 break;  
  49.             }  
  50.         }  
  51.           
  52.     }  
  53.        
  54. }  

加入一个权限,震动权限

  1. <uses-permission android:name="android.permission.VIBRATE"></uses-permission>  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值