android多功能按钮二——基础编

很开心写了第二个多功能按钮使用Notification编写,实现方法与上一次的功能差不多(每一个按钮都会有相应的提示。至于方法,我都写在了Activity里面。欢迎大家下载参考)。是基础编。先让我们看一下图先。

Main.java代码

package com.smart.activity;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity implements OnClickListener{
	private NotificationManager nm;
	//使用默认的方法
	private void setDefaults(String tickerText,String contentTitile,String contentText,int id,int resId,int defaults){
		Notification notification=new Notification(resId,tickerText,System.currentTimeMillis());
		PendingIntent conIntent=PendingIntent.getActivity(this, 0, new Intent(this,Main.class), 0);
		notification.setLatestEventInfo(this, contentTitile, contentText, conIntent);
		notification.defaults=defaults;
		nm.notify(id, notification);
	
	
	}
	
	//心情方法
   private void showNotification(String tickerText,String contentTitle,String contentText,int id,int resId){
	   Notification notification=new Notification(resId,tickerText,System.currentTimeMillis());
	   PendingIntent contentTntent=PendingIntent.getActivity(this, 0, getIntent(), 0);
	   
	   notification.setLatestEventInfo(this, contentTitle, contentText, contentTntent);
	   nm.notify(id,  notification);
   }
	
	
	

	@Override
	public void onClick(View v) {
		switch(v.getId()){//根据点击不同的按钮,弹出相应的提示
		case R.id.btnSmile:
			showNotification("情人节到了,我想你!", "情人节到了,我想你!",
						"情人节到了,与你一起度过,yeah!", R.drawable.smile,
						R.drawable.smile);
			break;
		case R.id.btnWhy:
			showNotification("情人节,你没送礼物给我", "所以心情不好。", "以后要记得",
					R.drawable.why, R.drawable.wrath);
			break;
		case R.id.btnClear:
			nm.cancelAll();
			break;
		case R.id.btnRing:
			setDefaults("使用默认的声音", "使用默认的声音", "使用默认的声音", R.id.btnRing, R.drawable.smile,
					Notification.DEFAULT_SOUND);
			break;
		case R.id.btnVibrate:
			setDefaults("使用默认的震动", "使用默认的震动", "使用默认的震动", R.id.btnVibrate, R.drawable.smile,
					Notification.DEFAULT_SOUND);
			break;
		case R.id.btnLight:
			setDefaults("使用默认的Light", "使用默认的Light", "使用默认的Light", R.id.btnLight, R.drawable.smile,
					Notification.DEFAULT_SOUND);
			break;
		case R.id.btnRingAndVibrate:
			setDefaults("所有的都使用默认值", "所有的都使用默认值", "所有的都使用默认值", R.id.btnRingAndVibrate, R.drawable.smile,
					Notification.DEFAULT_SOUND);
			break;
			
		}
	}

	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
	        
	        nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
	        Button btnSmile=(Button)findViewById(R.id.btnSmile);
	        Button btnWhy=(Button)findViewById(R.id.btnWhy);
	        Button btnWrath=(Button)findViewById(R.id.btnWrath);
	        Button btnClear=(Button)findViewById(R.id.btnClear);
	        Button btnRing=(Button)findViewById(R.id.btnRing);	
	        Button btnVibrate=(Button)findViewById(R.id.btnVibrate);
	        Button btnLight=(Button)findViewById(R.id.btnLight);
	        Button btnRingAndVibrate=(Button)findViewById(R.id.btnRingAndVibrate);
	        btnSmile.setOnClickListener(this);
	        btnWrath.setOnClickListener(this);
	        btnClear.setOnClickListener(this);
	        btnRing.setOnClickListener(this);
	        btnVibrate.setOnClickListener(this);
	        btnLight.setOnClickListener(this);
	        btnRingAndVibrate.setOnClickListener(this);
	 
	 
	 
	 }
}

main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
    <Button
    android:id="@+id/btnSmile"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="我想你"
     android:drawableLeft="@drawable/smile"
    />
    
       <Button
       android:id="@+id/btnWhy"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="为什么想你"
       android:drawableLeft="@drawable/why"
    />
       <Button
         android:id="@+id/btnWrath"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="心情不好"
     android:drawableLeft="@drawable/wrath"
    />
    
       <Button
       android:id="@+id/btnRing"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="默认的声音"
    />
    
         <Button
       android:id="@+id/btnVibrate"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="默认的震动"
    />
    
        <Button
       android:id="@+id/btnLight"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="使用默认的Light"
    />  
      <Button
       android:id="@+id/btnRingAndVibrate"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="使用默认值"
    />
    <Button
       android:id="@+id/btnClear"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="消除通知"
    />

</LinearLayout>

posted on 2011-02-16 16:04  llb988  阅读(1105)  评论(0编辑  收藏  举报

导航