main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/mytext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的登录状态:"/> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
.java 代码如下:
package org.lxh.demo;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnFocusChangeListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.Spinner;import android.widget.TextView;public class Hello extends Activity { private NotificationManager myNotification; private Spinner spinner; private ArrayAdapter<String> myAdapter; private static final String[] status={"在线","离开","忙碌","马上回来","离线"}; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 生命周期方法 super.setContentView(R.layout.main); // 设置要使用的布局管理器 this.myNotification=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); this.myAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,this.status); this.spinner=(Spinner)super.findViewById(R.id.spinner); this.spinner.setAdapter(myAdapter); this.spinner.setOnItemSelectedListener(new Item()); } private class Item implements OnItemSelectedListener{ private void setNotiType(int iconId, String text) { Intent notifyIntent=new Intent(Hello.this,Test.class); notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent appIntent=PendingIntent.getActivity(Hello.this, 0, notifyIntent, 0); Notification noti=new Notification(); noti.icon=iconId; noti.tickerText=text; noti.defaults=Notification.DEFAULT_SOUND; noti.setLatestEventInfo(Hello.this, "MSN登录状态", text, appIntent); myNotification.notify(0,noti); } public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { if(Hello.this.status[position].equals("在线")){ setNotiType(R.drawable.msn,"在线"); }else if(Hello.this.status[position].equals("离开")){ setNotiType(R.drawable.away,"离开"); } else if(Hello.this.status[position].equals("忙碌")){ setNotiType(R.drawable.busy,"忙碌"); } else if(Hello.this.status[position].equals("马上回来")){ setNotiType(R.drawable.min,"马上回来"); } else { setNotiType(R.drawable.offine,"离线"); } } public void onNothingSelected(AdapterView<?> arg0) { } }}
Test.java代码如下:
package org.lxh.demo;import android.app.Activity;import android.os.Bundle;import android.widget.Toast;public class Test extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(this, "这是模拟MSN切换登录状态的程序", Toast.LENGTH_SHORT); finish(); } }
配置权限:<activity android:name=".Test"></activity>
运行如下: