需求:
选择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;
}
}