`
canofy
  • 浏览: 829766 次
  • 性别: Icon_minigender_1
  • 来自: 北京、四川
社区版块
存档分类
最新评论

android弹出框2(相当于通知)

阅读更多
可以让其显示一会儿然后消失
也可以隔一段时间不断显示

第一种方式
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.app;

import com.example.android.apis.R;

import android.app.Activity;
import android.widget.Button;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

/**
 * When you push the button on this Activity, it creates a {@link Toast} object and
 * using the Toast method.
 * @see Toast
 * @see Toast#makeText(android.content.Context,int,int)
 * @see Toast#makeText(android.content.Context,java.lang.CharSequence,int)
 * @see Toast#LENGTH_SHORT
 * @see Toast#LENGTH_LONG
 */
public class NotifyWithText extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.notify_with_text);

        Button button;

        // short notification
        button = (Button) findViewById(R.id.short_notify);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                // Note that we create the Toast object and call the show() method
                // on it all on one line.  Most uses look like this, but there
                // are other methods on Toast that you can call to configure how
                // it appears.
                //
                // Note also that we use the version of makeText that takes a
                // resource id (R.string.short_notification_text).  There is also
                // a version that takes a CharSequence if you must construct
                // the text yourself.
                Toast.makeText(NotifyWithText.this, R.string.short_notification_text,
                    Toast.LENGTH_SHORT).show();
            }
        });

        // long notification
        // The only difference here is that the notification stays up longer.
        // You might want to use this if there is more text that they're going
        // to read.
        button = (Button) findViewById(R.id.long_notify);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(NotifyWithText.this, R.string.long_notification_text,
                    Toast.LENGTH_LONG).show();
            }
        });





    }
}



第二种方式
package com.example.android.apis.app;

// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import com.example.android.apis.R;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.SystemClock;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import java.util.Calendar;

/**
 * Example of scheduling one-shot and repeating alarms.  See
 * {@link OneShotAlarm} for the code run when the one-shot alarm goes off, and
 * {@link RepeatingAlarm} for the code run when the repeating alarm goes off.
 * <h4>Demo</h4>
App/Service/Alarm Controller
 
<h4>Source files</h4>
<table class="LinkTable">
        <tr>
            <td class="LinkColumn">src/com.example.android.apis/app/AlarmController.java</td>
            <td class="DescrColumn">The activity that lets you schedule alarms</td>
        </tr>
        <tr>
            <td class="LinkColumn">src/com.example.android.apis/app/OneShotAlarm.java</td>
            <td class="DescrColumn">This is an intent receiver that executes when the
                one-shot alarm goes off</td>
        </tr>
        <tr>
            <td class="LinkColumn">src/com.example.android.apis/app/RepeatingAlarm.java</td>
            <td class="DescrColumn">This is an intent receiver that executes when the
                repeating alarm goes off</td>
        </tr>
        <tr>
            <td class="LinkColumn">/res/any/layout/alarm_controller.xml</td>
            <td class="DescrColumn">Defines contents of the screen</td>
        </tr>
</table>

 */
public class AlarmController extends Activity {
    Toast mToast;

    @Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.alarm_controller);

        // Watch for button clicks.
        Button button = (Button)findViewById(R.id.one_shot);
        button.setOnClickListener(mOneShotListener);
        button = (Button)findViewById(R.id.start_repeating);
        button.setOnClickListener(mStartRepeatingListener);
        button = (Button)findViewById(R.id.stop_repeating);
        button.setOnClickListener(mStopRepeatingListener);
    }

    private OnClickListener mOneShotListener = new OnClickListener() {
        public void onClick(View v) {
            // When the alarm goes off, we want to broadcast an Intent to our
            // BroadcastReceiver.  Here we make an Intent with an explicit class
            // name to have our own receiver (which has been published in
            // AndroidManifest.xml) instantiated and called, and then create an
            // IntentSender to have the intent executed as a broadcast.
            Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
            PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
                    0, intent, 0);

            // We want the alarm to go off 30 seconds from now.
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 30);

            // Schedule the alarm!
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

            // Tell the user about what we did.
            if (mToast != null) {
                mToast.cancel();
            }
            mToast = Toast.makeText(AlarmController.this, R.string.one_shot_scheduled,
                    Toast.LENGTH_LONG);
            mToast.show();
        }
    };

    private OnClickListener mStartRepeatingListener = new OnClickListener() {
        public void onClick(View v) {
            // When the alarm goes off, we want to broadcast an Intent to our
            // BroadcastReceiver.  Here we make an Intent with an explicit class
            // name to have our own receiver (which has been published in
            // AndroidManifest.xml) instantiated and called, and then create an
            // IntentSender to have the intent executed as a broadcast.
            // Note that unlike above, this IntentSender is configured to
            // allow itself to be sent multiple times.
            Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
            PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
                    0, intent, 0);
            
            // We want the alarm to go off 30 seconds from now.
            long firstTime = SystemClock.elapsedRealtime();
            firstTime += 15*1000;

            // Schedule the alarm!
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                            firstTime, 15*1000, sender);

            // Tell the user about what we did.
            if (mToast != null) {
                mToast.cancel();
            }
            mToast = Toast.makeText(AlarmController.this, R.string.repeating_scheduled,
                    Toast.LENGTH_LONG);
            mToast.show();
        }
    };

    private OnClickListener mStopRepeatingListener = new OnClickListener() {
        public void onClick(View v) {
            // Create the same intent, and thus a matching IntentSender, for
            // the one that was scheduled.
            Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
            PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
                    0, intent, 0);
            
            // And cancel the alarm.
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.cancel(sender);

            // Tell the user about what we did.
            if (mToast != null) {
                mToast.cancel();
            }
            mToast = Toast.makeText(AlarmController.this, R.string.repeating_unscheduled,
                    Toast.LENGTH_LONG);
            mToast.show();
        }
    };
}


分享到:
评论

相关推荐

    Android自定义弹出框实现(修改版)完整实例源码

    首先,我们要理解Android中的弹出框(Dialog)是一种轻量级的窗口,它浮于应用程序主窗口之上,用于展示临时信息或进行简单的交互操作。Android SDK提供了多种预定义的Dialog样式,如AlertDialog、ProgressDialog等...

    android自定义弹出框实现(修改版).zip

    在Android开发中,自定义弹出框是一种常见的需求,它能提供更加个性化和灵活的交互方式。本项目“android自定义弹出框实现(修改版)”是一个针对原生Android弹出框进行定制化改造的例子,主要涉及Java编程语言。...

    弹出单选框示例(android)

    在Android开发中,创建弹出单选框是常见的交互设计,用于向用户展示有限的选项并获取他们的选择。本文将详细讲解如何实现一个弹出单选框的示例,其中包括如何选择将文本内容通过短信、邮件或剪贴板输出。 首先,...

    android弹出框和加载等待

    在Android开发中,用户体验是至关重要的,而"android弹出框和加载等待"就是提升用户体验的重要手段之一。本文将深入探讨这两个概念,并提供实现自定义弹出框和加载等待效果的方法。 首先,让我们来理解一下Android...

    Android:动画实现精美的弹出框(仿易信)

    2. **自定义PopupWindow**:在Android中,我们可以使用PopupWindow类来实现弹出框。首先,我们需要实例化PopupWindow,并设置其宽度、高度和内容视图。同时,可以设置PopupWindow的背景透明度,以便于实现半透明效果...

    Android炫酷弹出框LemonBubble控件

    Android炫酷弹出框LemonBubble控件

    android仿QQ底部弹出框选择头像

    在Android应用开发中,创建一个类似QQ的底部弹出框选择头像的功能涉及到多个关键知识点。这个功能允许用户选择头像,既可以打开相机拍摄新照片,也可以从本地图库选取已有图片,并提供图像剪切功能以调整图片大小。...

    android自定义弹出框

    android自定义弹出框android自定义弹出框android自定义弹出框android自定义弹出框android自定义弹出框android自定义弹出框android自定义弹出框android自定义弹出框android自定义弹出框android自定义弹出框android...

    android蓝牙调用弹出框

    android蓝牙使用,弹出框选择,获取名字和地址

    Android 自定义弹出框

    在Android应用开发中,弹出框(Dialog)是一种常见的用户交互元素,用于向用户展示临时信息或进行简单的交互操作。自定义弹出框允许开发者根据应用程序的风格和需求,定制弹出框的外观和功能,使其更加符合用户体验...

    android自定义弹出框实现

    在Android开发中,自定义弹出框是一种常见的需求,它能提供更加个性化和与应用风格一致的用户体验。本文将深入探讨如何在Android中实现自定义弹出框,并以"CustomDialog"为例,介绍其实现过程。 首先,Android系统...

    动态的弹出框,可以自定义布局,使用简单

    一个动态的弹出框,可以做动态提示什么的,自定义布局放入就可以。使用非常简单,并且做了性能上的优化,内存占用较小。但是发现个长时间弹出内存会升高,暂未发现原因

    模拟弹出框

    2. 一致性:保持弹出框的外观和行为与应用的整体风格一致,提供一致的用户体验。 3. 可访问性:确保弹出框对所有用户友好,包括视力障碍或其他障碍的用户,遵循无障碍设计原则。 4. 适当性:只在必要时使用弹出框,...

    android加载页面弹出框

    在Android应用开发中,"加载页面弹出框"是一个常见的用户界面元素,它通常用于在进行后台数据加载或处理时向用户提供反馈,显示一个正在旋转的菊花或者其他的进度指示符,让用户知道应用正在进行操作,而不是卡住...

    android自定义弹出框样式的实现方法

    做项目时,感觉android自带的弹出框样式比较丑,很多应用都是自己做的弹出框,这里也试着自己做了一个。 废话不说先上图片: 实现机制 1.先自定义一个弹出框的样式 2.自己实现CustomDialog类,继承自Dialog,实现...

    安卓-三种类型的弹出框

    在安卓应用开发中,弹出框...而基于Activity的自定义弹出框则适用于构建复杂功能的弹窗。根据实际需求,开发者可以选择最适合的弹出框类型。在项目中,SimpleDemo可能包含了这些弹出框的示例代码,供开发者参考和学习。

    安卓PrivacyShow隐私弹出框.zip

    "安卓PrivacyShow隐私弹出框.zip" 提供了一个具体的代码实例,帮助开发者在Android应用程序中实现隐私政策弹出框,确保用户在使用应用之前明确了解并同意隐私条款。 隐私弹出框通常包含以下几个关键部分: 1. **...

    Android自定义底部弹出框ButtomDialog

    Android自定义底部弹出框ButtomDialog Android自定义底部弹出框ButtomDialog是一种常用的Android用户界面控件,它可以在应用程序中提供一个弹出式的菜单或对话框,以便用户更方便地进行操作。下面将详细介绍Android...

    Android 点击listview弹出popupwindow弹出框

    本篇将详细介绍如何在用户点击`ListView`时弹出一个包含`PopupWindow`的弹出框,并且这个`PopupWindow`内还具有`ListView`的右滑删除功能。 首先,我们需要创建`PopupWindow`。`PopupWindow`的构造函数通常接收三个...

    Android自定义弹出多选框,可全选

    本教程将聚焦于如何在Android应用中实现一个自定义的弹出多选框,并实现全选功能。这个自定义组件可以用于让用户在一组选项中进行多项选择,如设置、过滤等场景。 首先,我们需要创建一个新的布局文件来设计多选框...

Global site tag (gtag.js) - Google Analytics