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

第一种方式
/* * 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;    @Overrideprotected 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();        }    };}

更多相关文章

  1. JNI基础实验一:调用.so文件--友善之臂Tiny210 android 串口/pwm/A
  2. 【Android(安卓)开发教程】经过预定义的查询字符串常量
  3. Android开发6:日志信息输出
  4. Android(安卓)Jetpack Compose总结
  5. activity以dialog形式显示
  6. android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件
  7. 在android上使用ASCII显示特殊符号
  8. Android自定义Toast,并解决toast不重复显示
  9. xml-----属性收集

随机推荐

  1. Linux 下使用 NDK 编译 protobuf 2.6.1
  2. Android(安卓)ExpandableListView开发简
  3. Android(安卓)android.uid.shared MK编译
  4. Android(安卓)Maven 采用第三方jar包,程序
  5. BitmapFactory类
  6. Android应用程序四大组件
  7. android的一些开发的资源
  8. Android入门:SQLite
  9. android 短信监听
  10. Android(安卓)菜单详解