android sdk --- Notification用法实例

本文介绍了一个基于Spinner组件的状态切换功能实现方法。当用户从Spinner下拉菜单中选择不同的选项时,如在线、离开或忙碌,状态栏会显示相应的图标和文字提示。此功能通过监听Spinner的选择变化,并使用NotificationManager来更新状态栏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:

选择spinner的下拉菜单,状态栏中出现对应图标和文字

 

代码:

public class MainActivity extends Activity {

 private NotificationManager myNotiManager;
 private Spinner mySpinner;
 private ArrayAdapter<String> myAdapter;
 private static final String[] status =
  {"on line", "leave", "busy"};
 
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  myNotiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  mySpinner = (Spinner)findViewById(R.id.spinner1);
  myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,status);
  //myAdapter.setDropDownViewResource(R.layout.myspinner_dropdown);
  mySpinner.setAdapter(myAdapter);
  
  mySpinner.setOnItemSelectedListener(
    new Spinner.OnItemSelectedListener()
  {

   @Override
   public void onItemSelected(AdapterView<?> arg0, View arg1,
     int arg2, long arg3) {
    // TODO Auto-generated method stub
    if(status[arg2].equals("on line"))
    {
     setNotiType(R.drawable.left, "on line");
    }
    else if(status[arg2].equals("leave"))
    {
     setNotiType(R.drawable.oa, "leave");
    }
    else if(status[arg2].equals("busy"))
    {
     setNotiType(R.drawable.right, "busy");
    }
   }

   @Override
   public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    
   }
      
  });
 }
 private void setNotiType(int iconId, String text)
 {
  Intent notifyIntent = new Intent(this, syAct.class);
  notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  PendingIntent appIntent = PendingIntent.getActivity(MainActivity.this, 0, notifyIntent, 0);
  Notification myNoti = new Notification();
  myNoti.icon= iconId;
  myNoti.tickerText = text;
  myNoti.defaults = Notification.DEFAULT_SOUND;
  myNoti.setLatestEventInfo(MainActivity.this, "login status", text, appIntent);
  myNotiManager.notify(0, myNoti);
 }
 @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;
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值