欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Notification应用 博客分类: android androidnotification 

程序员文章站 2024-03-14 22:31:11
...
package com.example.notificationtest;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

	NotificationManager notificationManager;
	Spinner spinner;
	String[] arr = { "请选择", "face", "folder", "other", "text", "why" };
	ArrayAdapter<String> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		spinner = (Spinner) findViewById(R.id.spinner);
		adapter = new ArrayAdapter<String>(MainActivity.this,
				android.R.layout.simple_list_item_1, arr);
		adapter.setDropDownViewResource(R.layout.text);
		spinner.setAdapter(adapter);

		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				
				if (position == 1) {
					setNotifi(R.drawable.face, arr[1]);
				}
				if (position == 2) {
					setNotifi(R.drawable.folder, arr[2]);
				}
				if (position == 3) {
					setNotifi(R.drawable.other, arr[3]);
				}
				if (position == 4) {
					setNotifi(R.drawable.txt, arr[4]);
				}
				if (position == 5) {
					setNotifi(R.drawable.why, arr[5]);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
			}
		});
	}

	private void setNotifi(int id, String text) {
		Intent intent = new Intent(this, Test.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent pend = PendingIntent.getActivity(MainActivity.this, 0,
				intent, 0);
		Notification notification = new Notification();
		notification.icon = id;
		notification.tickerText = text;
		notification.defaults = Notification.DEFAULT_SOUND;
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		notification.setLatestEventInfo(MainActivity.this, "MSN", text, pend);
		notificationManager.notify(0, 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.main, menu);
		return true;
	}

}