`
yejun0099
  • 浏览: 25119 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Notification应用

阅读更多
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;
	}

}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics