public class MainActivity extends Activity { private Spinner spinner = null; private NotificationManager notificationManager = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = (Spinner) findViewById(R.id.spinner1); ArrayAdapter spinnerAdapter = ArrayAdapter.createFromResource( MainActivity.this, R.array.spinnerItems, android.R.layout.simple_spinner_item); spinnerAdapter .setDropDownViewResource(android.R.layout.simple_list_item_checked); spinner.setAdapter(spinnerAdapter); spinner.setSelection(1, true); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub if (arg0.getItemAtPosition(arg2).toString().equals("Online")) { showNotification("Online"); } else { // showNotification("Outline"); showDefinedNotifications(); } } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } /** * 显示系统预定义的通知栏 * * @param string */ private void showNotification(String string) { notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(); notification.when = System.currentTimeMillis(); notification.tickerText = "show";// notification刚刚生成时弹出的提示文字 notification.icon = R.drawable.acc_on; notification.flags = Notification.FLAG_AUTO_CANCEL;// 可以清除 // notification.defaults = Notification.DEFAULT_VIBRATE;//添加震动,记得加权限 // long[] vibrate = {0,100,200,300}; // notification.vibrate = vibrate; Intent intent = new Intent(MainActivity.this, MainActivity.class); // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent .getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(MainActivity.this, "Notification_test", string, pendingIntent); notificationManager.notify(1, notification); } /** * 显示自定义的通知栏 * 使用自定义的点击通知后才会消失。 */ private void showDefinedNotifications() { notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.acc_on, "有提示!", System.currentTimeMillis()); Intent intent = new Intent(MainActivity.this, MainActivity.class); notification.flags = Notification.FLAG_AUTO_CANCEL; PendingIntent pendingIntent = PendingIntent .getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_items); remoteViews.setImageViewResource(R.id.imageView1, R.drawable.ic_launcher); remoteViews.setTextViewText(R.id.tx_content, "我是自定义的Notification"); notification.contentView = remoteViews; notification.contentIntent = pendingIntent; notificationManager.notify(1, notification); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); } }
自定义的R.layout.notification_items:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tx_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> </LinearLayout>
自定义数组array.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="spinnerItems"> <item>Online</item> <item>Outline</item> </string-array> </resources>
结构图: