Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android--Notification

Android--Notification

編輯:關於Android編程

main.xml代碼如下:

 

 

.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  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(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();
	}
	

}

配置權限:

 

運行如下:

\\

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved