首页产品库评测行情新闻|手机数码笔记本台式机DIY硬件数字家庭数码相机办公外设|软件下载游戏开发|社区

更多

数码相机
MP4
LCD
机箱
音箱

软件资讯设计 工具 系统 开发 安全 办公 陶吧 IT教育 Vista频道 | 下载中心酷我音乐盒 腾讯QQ
天极网 > 开发频道>Android使用Toast和Notification代码

Android使用Toast和Notification代码

2012-01-09 11:19作者: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);
    }
}

关注此文的读者还看过:

返回开发频道首页

共2页。 12下一页
网友讨论

软件频道最新更新

热点推荐

编辑推荐

软件下载

热门
推荐

网友关注

软件
资料
游戏

装机推荐

文章排行

本周
本月
最新更新
天极服务|关于我们|About us|网站律师|RSS订阅|友情合作|加入我们|天极动态|网站地图|意见反馈|MSN/QQ上看天极
Copyright (C) 1999-2012 Yesky.com, All Rights Reserved 版权所有 天极网络