Android APIDemo 之 019_IncomingMessage

ApiDemo的第一个应用:CustomDialog

 

运行效果:
Android APIDemo 之 019_IncomingMessage

 

NotificationActivity.java

/*
 * Notification的特殊应用
 * 参考好文章:http://blog.csdn.net/wukunting/archive/2010/06/10/5661769.aspx
 * 
 */
package com.lgl;

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.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class NotificationActivity extends Activity {
	
	private Button btn1;   
    private Notification notification;   
    private NotificationManager notificationManager;   
    private Intent intent;   
    private PendingIntent pendIntent;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        showNotification();
        
        btn1 = (Button) findViewById(R.id.btn01); 
        
        btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//显示自定义的Toast
				showToast();
				// 设置通知显示的参数
				notification.setLatestEventInfo(NotificationActivity.this
						, "Button1"		//通知点击进去后显示 的标题
						, "Button1通知"	//通知点击进去后显示 的内容(在标题下方)
						, pendIntent);	//指定跳转到的Activity
				
				/* 设置振动的样式
				 * 需要在 AndroidManifest.xml 配置文件中添加权限
				 * <uses-permission android:name="android.permission.VIBRATE" />
				 * 以下的参数是:振动100ms,停止250ms,振动100ms,停止500ms
				 * */
				notification.vibrate = new long[] { 100, 250, 100, 500, 1000};
				
				/* 执行通知,第一个参数为通知的唯一标识。
					当通知被点击后,通知将被取消,而取消就是通过这个唯一标识来取消的*/
				notificationManager.notify(R.drawable.star_big_on, notification);
			}
		});
        
    }
    
    /**
     * 设置布局
     * @param resource
     * @return
     */
    private View inflateView(int resource) {
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return vi.inflate(resource, null);
    }
    
    /**
     * 显示一个自定义的Toast,里面有图片
     */
    protected void showToast() {
        // 创建一个View,并加载布局方式
        View view = inflateView(R.layout.incoming_message_panel);

        // set the text in the view
        TextView tv = (TextView)view.findViewById(R.id.message);
        tv.setText("khtx. meet u for dinner. cul8r");

        // 定义一个Toast,并显示
        Toast toast = new Toast(this);
        toast.setView(view);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }
    
    /**
     * 显示通知
     */
    public void showNotification() {
    	// 获取系统服务(消息管理)
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // 点击通知时转移内容 
        intent = new Intent(NotificationActivity.this, Activity2.class);
        // 设置点击通知时显示内容的类
        pendIntent = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);
        
        /*
         * 第一种:实例化这个通知时,
         *        可是使用下面的先实例一个空的Notification,然后在设置图标、内容等等
         *        notification = new Notification();
         * 
         * 
         * 第二种:实例化这个通知时,
         *        直接使用带参数实例化,
         *        notification = new Notification(R.drawable.star_big_on, "tickerText", System.currentTimeMillis());
         *        这样的话,就不用在另外的设置图标和内容了
         */
        notification = new Notification();
        //  设置在状态栏显示的图标   
		notification.icon = R.drawable.star_big_on;
		//  设置在状态栏显示的内容
		notification.tickerText = "Button1通知内容.......";
		//  默认的声音
		notification.defaults = Notification.DEFAULT_SOUND;
	}
}

 
 Activity2.java

package com.lgl;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;

public class Activity2 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.incoming_message_view);
        
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //取消这个通知
        nm.cancel(R.drawable.star_big_on);
    }
}

 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"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button  
	android:id="@+id/btn01"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content"
	android:text="Notification"
	/>
</LinearLayout>

 

incoming_message_panel.xml

<?xml version="1.0" encoding="utf-8"?>


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/toast_frame">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/frog"
                />

            <TextView
                android:id="@+id/message"
                android:layout_gravity="center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="6dip"
                />

    </LinearLayout>
</FrameLayout>

 

incoming_message_view.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"
    android:paddingLeft="4dip"
    android:paddingRight="4dip"
    android:paddingTop="4dip"
    >

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/sample_thumb_2"
                    />

                <TextView
                    android:id="@+id/message"
                    android:layout_gravity="center_vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="6dip"
                    android:text="incoming_message_view_message_text"
                    />

        </LinearLayout>

        <TextView
            android:id="@+id/message"
            android:layout_gravity="center_vertical"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
            android:paddingTop="12dip"
            android:text="imcoming_message_view_message2_text"
            />

</LinearLayout>

 

你可能感兴趣的:(android,xml,.net,OS,配置管理)