首页 > 其他分享 >Android 控件之Notification

Android 控件之Notification

时间:2023-05-16 20:31:47浏览次数:36  
标签:控件 NotificationManager Notification noticed import Android com android


[quote]
最近一直在研究android,并一边研究一边做应用。其中遇到了把程序通知常驻在Notification栏,并且不能被clear掉的问题。虽然notify()的第一个参数可以写死并clear掉,但这个值我并不想写死,但是这个值如果是随机生成一个数怎么传给Activity,用Intent当中的Bundle是可以传值过去,但是一个Notification消息是没问题,一但有多个消息,这个值就不管用了,大家有什么好的方法?
[/quote]

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:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/button"
		android:text="显示消息提示" />
</LinearLayout>



notification_layout.xml布局文件:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="wrap_content">
	<EditText android:text="启动Notification" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:editable="false"
		android:cursorVisible="false" android:layout_gravity="center_horizontal" />
</LinearLayout>



IaiaiActivity.java类:


package com.iaiai.activity;

import java.util.Date;
import java.util.Random;

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;

/**
 * 
 * <p>
 * Title: IaiaiActivity.java
 * </p>
 * <p>
 * E-Mail: [email protected]
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-6-26
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class IaiaiActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button button = (Button) findViewById(R.id.button);
		View.OnClickListener listener = new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				PendingIntent pending = PendingIntent.getActivity(
						IaiaiActivity.this, 0, new Intent(IaiaiActivity.this,
								NotificationActivity.class), 0);

				Notification noticed = new Notification();
				noticed.icon = R.drawable.icon;
				noticed.tickerText = "状态栏通知";

				// noticed.defaults = Notification.DEFAULT_SOUND; // 使用默认的声音
				// noticed.defaults = Notification.DEFAULT_VIBRATE; // 使用默认的震动
				// noticed.defaults = Notification.DEFAULT_LIGHTS; // 使用默认的Light
				// noticed.defaults = Notification.DEFAULT_ALL; // 所有的都使用默认值
				noticed.defaults = Notification.DEFAULT_SOUND;

				//设置音乐
				// noticed.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
				// noticed.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

				noticed.setLatestEventInfo(IaiaiActivity.this, "这是一个状态栏通知",
						"查看", pending);
				NotificationManager noticedManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
				noticedManager.notify(0, noticed);
			}
		};
		button.setOnClickListener(listener);
	}

	public int getRand() {
		Date date = new Date();
		int year = date.getYear();
		int month = date.getMonth();
		int day = date.getDate();
		return 0;
	}

}



NotificationActivity.java类:


package com.iaiai.activity;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;

/**
 * 
 * <p>
 * Title: NotificationActivity.java
 * </p>
 * <p>
 * E-Mail: [email protected]
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-6-26
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class NotificationActivity extends Activity {

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

		setContentView(R.layout.notification_layout);

		Bundle bundle = getIntent().getExtras();

		NotificationManager noticedManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		noticedManager.cancel(0);
	}

}



运行结果:


[img]http://dl.iteye.com/upload/attachment/505287/e52382b1-3f91-3474-886d-3d1a5710804f.jpg[/img]


[img]http://dl.iteye.com/upload/attachment/505288/584e667c-d404-37e2-8dad-86f9f23d78e4.jpg[/img]


标签:控件,NotificationManager,Notification,noticed,import,Android,com,android
From: https://blog.51cto.com/u_15955464/6286984

相关文章

  • Android Studio学习日志二,Toast用法
    首先,创建一个util,在ToastUtil里面调用方法在里面编写Toast类的代码,方便以后调用packagecom.example.appdemo.util;importandroid.content.Context;importandroid.widget.Toast;publicclassToastUtil{publicstaticToastmToast;publicstaticvoidsh......
  • 作为一个 Android 开发者,我为什么要在意深度学习?
    阅读本文大概需要5.20分钟。AlphaGo再次战胜人类,Google发布TensorFlow正式版,百度筹建深度学习实验室......人人都在谈论机器学习、深度学习,作为移动应用开发者,这些概念离你很远吗?其实不然,如果你不想被未来淘汰,想获得更好的工作,想在技术上变得更牛掰,机器学习和深度学习可能是......
  • Android Studio学习日志一,直接跳转和密码正确跳转
    记录一下我跟着手把手教你用AndroidStudio写一个APP_UP主Superior-Leo进行app开发小项目UP主的笔记首先,在资源文件创建一个文件夹,用来放APP背景图片,按钮图片一、直接跳转第一步、声明控件第二步,找到控件第三步骤,实现跳转代码packagecom.example.appdemo;importa......
  • Android Lottie动画库的导入与使用
    没想到又把安卓捡回来了,好长时间没碰这玩意但是大作业又不得不写......emmmm所以为了给大作业里的功能实现一个好看的UI界面还是得写个app出来。不少app在启动时或者干别的什么活动时会有一个动画效果,这时候可以用Lottie库实现。添加依赖在build.gradle(:app)的dependencies添......
  • Android消息机制——Handler、Looper、MessageQueue
    最近在做毕设,关于android的,其中觉得android的消息机制很有意思,这里就写下自己的想法和Windows一样android也是消息驱动的。Android通过Handler和looper实现消息循环机制。一、Handler的创建每个Handler都会和一个线程和线程的messagequeue关联起来,此时你可以传递messages和runna......
  • android开发mac系统搭建flutter开发环境
    安装xcodemac系统自带的AppStore里搜索xcode安装即可安装JDKJDK下载地址:https://www.oracle.com/java/technologies/downloads/需要登录Oracle账号才能下载下载dmg文件之后双击安装即可安装AndroidStudio开发工具AndroidStudio下载地址:https://developer.android.goog......
  • 第9-1讲,scale 范围控件
    为了提供友好的交互方式,你可以使用范围控件,假设用户在你的应用程序里选择商品,你可以给他提供一个价格范围选择控件,下面是效果图 我希望随着滑块的移动,黄色区域可以显示所选取的价格示例代码importtkinterastkwindow=tk.Tk()window.title('scale')window.geometry("5......
  • 多个 ComboBox 控件绑定同一数据源,数据会联动(其中一个选择项改变的时候,其余也会跟着变
    问题:在Winform开发中,两个ComboBox控件绑定了同一个数据源List<T>,但是在使用的时候发现,选择其中一个ComboBox的时候,另一个也会跟着变化。原因:内存中只有一份数据,改变任何一个ComboBox都会使得数据源有所变化,导致其他ComboBox的展示效果发生联动。解决:将数据源进行复制,相当于为每......
  • 【android】手机亮屏锁定(安卓亮屏解锁)
    1、Android屏幕常亮/点亮//保持屏幕常亮PowerManagerpm=(PowerManager)getSystemService(Context.POWER_SERVICE);mWakeLock=pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP,LOCK_TAG);mWakeLock.acquire();//释放屏幕常亮锁if(nu......
  • Android AVD创建及设置中各参数详解
    设置AVD时有些参数比较模糊,特地找了篇文章,大家参考下!本文根据如下的模拟器安装做一些解释:[color=red][b]Name[/b][/color]:自定义虚拟的名称,不能有空格或者其他非法字符,否则不能创建,即CreatAVD不能高亮点击。[color=red][b]Target[/b][/color]:选择要运行的android版本(也可理解......