技术开发 频道

Android使用Toast和Notification代码

  下面开始对上述代码进行讲解

  1)因为所有的Notification都是通过NotificationManager来管理的,所以应该首先创建NotificationManager对象实例,以便管理这个Activity中信息。

  mNotificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

  2)函数setWeather是ActivityMainNotification中的重要函数之一,它实例化了一个Notification,并将这个Notification显示出来。

  3)看下面的代码。

  Notification notification = new Notification(drawable, tickerText,

  System.currentTimeMillis());

  包含了3个参数,具体说明如下。

  第一个:要显示的图片的ID。

  第二个:显示的文本文字。

  第三个:Notification显示的时间,一般是立即显示,时间就是System.currentTimeMillis()。

  4)函数setDefault也是ActivityMainNotification.java中的一个重要函数,在此函数中初始化了一个Notification,在设置Notification时使用了其默认值的形式,即:

  notification.defaults = defaults;

  另外,在上述程序中还用到了以下几种表现形式。

  Notification.DEFAULT_VIBRATE:表示当前的Notification显示出来时手机会发出震动。

  Notification.DEFAULT_SOUND:表示当前的Notification显示出来时手机会伴随音乐。

  Notification.DEFAULT_ALL:表示当前的Notification显示出来时手机即会震动,也会伴随音乐。

  这样单击第一个Button后会执行上述处理程序,来到对应的新界面,新界面的布局文件是由activity_notification.xml实现的,其主要代码如下所示。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
="fill_parent"
    android:layout_height
="fill_parent">
    
    
<LinearLayout
        android:orientation
="vertical"
        android:layout_width
="fill_parent"
        android:layout_height
="wrap_content">
            
        
<LinearLayout
            android:orientation
="vertical"
            android:layout_width
="fill_parent"
            android:layout_height
="wrap_content">
        
            
<Button
                android:id
="@+id/sun_1"
                android:layout_width
="wrap_content"
                android:layout_height
="wrap_content"
                android:text
="适合户外活动" />
        
            
<Button
                android:id
="@+id/cloudy_1"
                android:layout_width
="wrap_content"
                android:layout_height
="wrap_content"
                android:text
="不太适合户外活动" />
                
              
<Button
                android:id
="@+id/rain_1"
                android:layout_width
="wrap_content"
                android:layout_height
="wrap_content"
                android:text
="一点也不适合户外活动" />
                
        
</LinearLayout>
        
        
<TextView
            android:layout_width
="wrap_content"
            android:layout_height
="wrap_content"
            android:layout_marginTop
="20dip"
            android:text
="高级notification" />

        
<LinearLayout
            android:orientation
="vertical"
            android:layout_width
="fill_parent"
            android:layout_height
="wrap_content">
        
            
<Button
                android:id
="@+id/defaultSound"
                android:layout_width
="wrap_content"
                android:layout_height
="wrap_content"
                android:text
="有声音的notification" />
        
            
<Button
                android:id
="@+id/defaultVibrate"
                android:layout_width
="wrap_content"
                android:layout_height
="wrap_content"
                android:text
="振动的notification" />
                
              
<Button
                android:id
="@+id/defaultAll"
                android:layout_width
="wrap_content"
                android:layout_height
="wrap_content"
                android:text
="声音+振动的notification" />
                
        
</LinearLayout>
        
        
<Button android:id="@+id/clear"
            android:layout_width
="wrap_content"
            android:layout_height
="wrap_content"
            android:layout_marginTop
="20dip"
            android:text
="清除notification" />
            
    
</LinearLayout>

</ScrollView>

  执行后新界面的效果如图6-64所示。

对上述代码进行讲解
▲图6-64 运行效果

  当单击新图6-64中的Button后,会根据上述处理文件实现某种效果,例如,单击“有声音的Notification”按钮后,会发出声音。

  第5步:编写第二个Button的处理程序,即单击图6-63中的“介绍Toast”按钮后,执行ActivityToast.java,其主要代码如下所示。

package com.eoeandroid.toast_and_notification;

import com.eoeandroid.toast_and_notification.R;

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 ActivityToast extends Activity {

    OnClickListener listener1
= null;
    OnClickListener listener2
= null;
    Button button1;
    Button button2;
    
private static int NOTIFICATIONS_ID = R.layout.activity_toast;

    @Override
    
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listener1
= new OnClickListener() {
            
public void onClick(View v) {
                setTitle(
"短时显示Toast");
                showToast(Toast.LENGTH_SHORT);

            }
        };
        listener2
= new OnClickListener() {
            
public void onClick(View v) {
                setTitle(
"长时显示Toast");
                showToast(Toast.LENGTH_LONG);
                showNotification();
            }
        };
        setContentView(R.layout.activity_toast);
        button1
= (Button) findViewById(R.id.button1);
        button1.setOnClickListener(listener1);
        button2
= (Button) findViewById(R.id.button2);
        button2.setOnClickListener(listener2);
    }

    protected void showToast(
int type) {

        View view
= inflateView(R.layout.toast);

        TextView tv
= (TextView) view.findViewById(R.id.content);
        tv.setText(
"欢迎加入爬山摄影户外俱乐部,强身健体、增强友谊,共创和谐社会!");
        
/*实例化Toast*/
        Toast toast
= new Toast(this);
        toast.setView(view);
        toast.setDuration(type);
        toast.show();
    }

    
private View inflateView(int resource) {
        LayoutInflater vi
= (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return vi.inflate(resource,
null);
    }

    protected void showNotification() {

        NotificationManager notificationManager
= (NotificationManager) getSystemService
(NOTIFICATION_SERVICE);

        CharSequence title
= "最专业的户外拓展俱乐部";
        CharSequence contents
= "户外合作论坛.com";

        PendingIntent contentIntent
= PendingIntent.getActivity(this, 0,
                
new Intent(this, ActivityMain.class), 0);

        Notification notification
= new Notification(R.drawable.default_icon,
                title, System.currentTimeMillis());

        notification.setLatestEventInfo(this, title, contents, contentIntent);

        
// 100ms延迟后,振动250ms,停止100ms后振动500ms
        notification.vibrate
= new long[] { 100, 250, 100, 500 };

        notificationManager.notify(NOTIFICATIONS_ID, notification);
    }
}

  上述处理程序是通过Toast实现的,它不需要用NotificationManager来管理,上述代码的处理流程如下。

  1)实例化Toast,每个Toast和一个View相关。

  2)设置Toast的长短,通过“showToast(Toast.LENGTH_SHORT);”来设置Toast短时间显示,通过“showToast(Toast.LENGTH_LONG);”来设置Toast长时间显示。

  3)通过函数showToast来显示Toast,具体说明如下。

  当单击“长时显示”按钮后,程序会长时间地显示提醒,并用Notification在状态栏中提示用户。

  当单击“短时显示”按钮后,程序会短时间的显示提醒。

  这样单击第二个Button后会执行上述处理程序,来到对应的新界面,新界面的布局文件是由activity_toast.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/button1"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content" android:text="短时显示Toast" />
    
<Button android:id="@+id/button2"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content" android:text="长时显示Toast" />
</LinearLayout>

  执行后新界面的效果如图6-65所示。

对上述代码进行讲解
▲图6-65 运行效果

对上述代码进行讲解
▲图6-66 运行效果

  当单击新图6-65中的Button后,会根据上述处理文件而实现某种效果,例如,单击“短时显示Toast”按钮后,会短时间显示一个提醒,当单击“长时显示Toast”按钮后,会长时间显示一个提醒,两种方式的提醒界面都是相同的,具体效果如图6-66所示。

  至此,整个实例介绍完毕,读者可以阅读随书光盘中的源代码,来了解更加具体的信息。

0
相关文章