歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 消息通知-Notification

Android 消息通知-Notification

日期:2017/3/1 11:16:17   编辑:Linux編程

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. public class NotificationTestActivity extends Activity {
  11. /** Called when the activity is first created. */
  12. private Button button01,button02;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. button01=(Button) findViewById(R.id.button1);
  18. button02=(Button) findViewById(R.id.button2);
  19. button01.setOnClickListener(new Mybutton());
  20. button02.setOnClickListener(new Mybutton());
  21. }
  22. class Mybutton implements OnClickListener{
  23. @Override
  24. public void onClick(View v) {
  25. // TODO Auto-generated method stub
  26. int FLAG=0;
  27. NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);//獲取系統NotificationManager服務
  28. Intent intent=new Intent();
  29. intent.setClass(getApplicationContext(), Notification.class);
  30. PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(), 0, intent, FLAG);
  31. Notification notification=new Notification();
  32. notification.when=System.currentTimeMillis();//發出這個通知的時間
  33. notification.defaults=Notification.DEFAULT_ALL;//提示方式,有震動,聲音,閃關燈
  34. switch (v.getId()) {
  35. case R.id.button1:
  36. notification.icon=R.drawable.alert_15;
  37. notification.setLatestEventInfo(NotificationTestActivity.this, "通知", "手機在此聯網", pendingIntent);
  38. notification.tickerText="聯網通知";
  39. manager.notify(0, notification);
  40. break;
  41. case R.id.button2:
  42. manager.cancel(FLAG);//刪除當前的notifcation
  43. break;
  44. default:
  45. break;
  46. }
  47. }
  48. }
  49. }

加入一個權限,震動權限

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

Copyright © Linux教程網 All Rights Reserved