EX05_08.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:3k
源码类别:

android开发

开发平台:

Java

  1. package irdc.ex05_08;
  2. /* import相关class */
  3. import android.app.Activity;
  4. import android.app.Notification;
  5. import android.app.NotificationManager;
  6. import android.app.PendingIntent;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.Spinner;
  13. public class EX05_08 extends Activity
  14. {
  15.   /*声明对象变量*/
  16.   private NotificationManager myNotiManager;
  17.   private Spinner mySpinner;
  18.   private ArrayAdapter<String> myAdapter;
  19.   private static final String[] status =
  20.   { "在线","离开","忙碌中","马上回来","离线" };
  21.   
  22.   @Override
  23.   protected void onCreate(Bundle savedInstanceState)
  24.   {
  25.     super.onCreate(savedInstanceState);
  26.     /* 载入main.xml Layout */
  27.     setContentView(R.layout.main);
  28.     
  29.     /* 初始化对象 */
  30.     myNotiManager=
  31.       (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  32.     mySpinner=(Spinner)findViewById(R.id.mySpinner);
  33.     myAdapter=new ArrayAdapter<String>(this,
  34.               android.R.layout.simple_spinner_item,status);
  35.     /* 应用myspinner_dropdown自定义下拉菜单模式 */
  36.     myAdapter.setDropDownViewResource(R.layout.myspinner_dropdown);
  37.     /* 将ArrayAdapter添加Spinner对象中 */
  38.     mySpinner.setAdapter(myAdapter);
  39.     /* 将mySpinner添加OnItemSelectedListener */
  40.     mySpinner.setOnItemSelectedListener(
  41.       new Spinner.OnItemSelectedListener()
  42.     {
  43.       @Override
  44.       public void onItemSelected(AdapterView<?> arg0,View arg1,
  45.                                  int arg2,long arg3)
  46.       {
  47.         /* 依照选择的item来判断要发哪一个notification */
  48.         if(status[arg2].equals("在线"))
  49.         {
  50.           setNotiType(R.drawable.msn,"在线");
  51.         }
  52.         else if(status[arg2].equals("离开"))
  53.         {
  54.           setNotiType(R.drawable.away,"离开");
  55.         }
  56.         else if(status[arg2].equals("忙碌中"))
  57.         {
  58.           setNotiType(R.drawable.busy,"忙碌中");
  59.         }
  60.         else if(status[arg2].equals("马上回来"))
  61.         {
  62.           setNotiType(R.drawable.min,"马上回来");
  63.         }
  64.         else
  65.         {
  66.           setNotiType(R.drawable.offine,"离线");
  67.         }
  68.       }
  69.       @Override
  70.       public void onNothingSelected(AdapterView<?> arg0)
  71.       {
  72.       }
  73.     });
  74.   }
  75.   
  76.   /* 发出Notification的method */
  77.   private void setNotiType(int iconId, String text)
  78.   {
  79.     /* 创建新的Intent,作为点击Notification留言条时,
  80.      * 会运行的Activity */ 
  81.     Intent notifyIntent=new Intent(this,EX05_08_1.class);  
  82.     notifyIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
  83.     /* 创建PendingIntent作为设置递延运行的Activity */ 
  84.     PendingIntent appIntent=PendingIntent.getActivity(EX05_08.this,
  85.                             0,notifyIntent,0); 
  86.     /* 创建Notication,并设置相关参数 */ 
  87.     Notification myNoti=new Notification();
  88.     /* 设置statusbar显示的icon */
  89.     myNoti.icon=iconId;
  90.     /* 设置statusbar显示的文字信息 */
  91.     myNoti.tickerText=text;
  92.     /* 设置notification发生时同时发出默认声音 */
  93.     myNoti.defaults=Notification.DEFAULT_SOUND;
  94.     /* 设置Notification留言条的参数 */
  95.     myNoti.setLatestEventInfo(EX05_08.this,"MSN登录状态",
  96.                               text,appIntent);
  97.     /* 送出Notification */
  98.     myNotiManager.notify(0,myNoti);
  99.   } 
  100. }