技术开发 频道

Android使用Toast和Notification代码

  【IT168技术】之前我们介绍了Android开发之:Toast和Notification概述部分,在本节的内容中,将通过一个具体实例的实现过程,来讲解联合使用Toast和Notification实现提醒功能效果的具体使用流程。本实例源代码保存在“光盘:\daima\6\toast_ and_notification”,具体实现流程如下。

  第1步:打开eclipse,依次单击“File”→“New”→“Android Project”,新建一个名为“toast_and_notification”的工程文件。

  第2步:编写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/button1"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content" android:text="介绍Notification" />
    
<Button android:id="@+id/button2"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content" android:text="介绍Toast" />
</LinearLayout>

  通过上述代码插入了两个Button按钮,执行后效果如图6-63所示。

Android使用Toast和Notification代码
▲图6-63 插入两个Button

  第3步:编写处理文件ActivityMain.java,具体代码如下所示。

package com.eoeandroid.toast_and_notification;
import com.eoeandroid.toast_and_notification.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityMain extends Activity {
    OnClickListener listener1
= null;
    OnClickListener listener2
= null;

    Button button1;
    Button button2;
    @Override
    
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listener1
= new OnClickListener() {
            
public void onClick(View v) {
                setTitle(
"介绍Notification");
                Intent intent
= new Intent(ActivityMain.this,
                        ActivityMainNotification.class);
                startActivity(intent);
            }
        };
        listener2
= new OnClickListener() {
            
public void onClick(View v) {
                setTitle(
"介绍Toast");
                Intent intent
= new Intent(ActivityMain.this,
                        ActivityToast.class);
                startActivity(intent);
            }
        };
        setContentView(R.layout.main);
        button1
= (Button) findViewById(R.id.button1);
        button1.setOnClickListener(listener1);
        button2
= (Button) findViewById(R.id.button2);
        button2.setOnClickListener(listener2);
    }
}

  在上述代码中,对两个Button绑定了单击监听器OnClickListener,当单击这两个Button时,会跳转到新的Activity上面。

  第4步:编写第一个Button的处理程序,即单击图6-64中的“介绍Notification”按钮后,执行ActivityMainNotification.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.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ActivityMainNotification extends Activity {
    
private static int NOTIFICATIONS_ID = R.layout.activity_notification;
    
private NotificationManager mNotificationManager;
    @Override
    
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        Button button;
        mNotificationManager
= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        button
= (Button) findViewById(R.id.sun_1);
        button.setOnClickListener(
new Button.OnClickListener() {
            
public void onClick(View v) {
                setWeather(
"适合户外", "天气状况", "适合户外", R.drawable.sun);
            }
        });

        button
= (Button) findViewById(R.id.cloudy_1);
        button.setOnClickListener(
new Button.OnClickListener() {
            
public void onClick(View v) {
                setWeather(
"不太适合", "天气状况", "不太适合", R.drawable.cloudy);
            }
        });

        button
= (Button) findViewById(R.id.rain_1);
        button.setOnClickListener(
new Button.OnClickListener() {
            
public void onClick(View v) {
                setWeather(
"不适合", "天气状况", "不适合", R.drawable.rain);
            }
        });

        button
= (Button) findViewById(R.id.defaultSound);
        button.setOnClickListener(
new Button.OnClickListener() {
            
public void onClick(View v) {
                setDefault(Notification.DEFAULT_SOUND);
            }
        });

        button
= (Button) findViewById(R.id.defaultVibrate);
        button.setOnClickListener(
new Button.OnClickListener() {
            
public void onClick(View v) {
                setDefault(Notification.DEFAULT_VIBRATE);
            }
        });

        button
= (Button) findViewById(R.id.defaultAll);
        button.setOnClickListener(
new Button.OnClickListener() {
            
public void onClick(View v) {
                setDefault(Notification.DEFAULT_ALL);
            }
        });

        button
= (Button) findViewById(R.id.clear);
        button.setOnClickListener(
new Button.OnClickListener() {
            
public void onClick(View v) {
                mNotificationManager.cancel(NOTIFICATIONS_ID);
            }
        });

    }

    
private void setWeather(String tickerText, String title, String content,
            
int drawable) {

        Notification notification
= new Notification(drawable, tickerText,
                System.currentTimeMillis());

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

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

        mNotificationManager.notify(NOTIFICATIONS_ID, notification);
    }

    
private void setDefault(int defaults) {

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

        
String title = "天气预报";
        
String content = "晴空万里";

        final Notification notification
= new Notification(R.drawable.sun,
                content, System.currentTimeMillis());

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

        notification.defaults
= defaults;

        mNotificationManager.notify(NOTIFICATIONS_ID, notification);
    }
}

 

0
相关文章