Android之OptionsMenu与Notification的实现
OptionsMenu是Android提供的一种菜单方式,我们知道当智能机刚兴起时,手机上都会有一个MENU(菜单键),当我们点击时,默认我们打开Android提供的默认菜单,本篇我么就一起来学一下,如何自定义Android MENU菜单。
当我们创建一个Activity后,默认实现了OnCreate方法,我们想实现Android菜单,还需要实现另外两个方法:onCreateOptionsMenu();onOptionsItemSelected(),下面我们就一起来学习一下如何使用吧;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "分享");
menu.add(0, 1, 1, "关于");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
//调用发短信功能
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
intent.putExtra(Intent.EXTRA_TEXT, "I would like to share this with you...");//短信内容
startActivity(Intent.createChooser(intent, getTitle()));
break;
case 1:
Toast.makeText(MainActivity.this, "祝你开心愉快!", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
}
好了,我们的Android MENU菜单就实现了,大家快来试一下吧。
下面我们一起学习一下Android通知系统Notification:
- 要使用Android通知必须使用到Android通知管理器:NotificationManager管理这个应用程序的通知,每个Notification都有唯一标识符即ID。用于管理更新这个通知内容……
第一种:实现方式(Notification通知系统默认提示方式)
Notification实例和别的应用不同,没有布局文件,这里我只在布局文件中添加了一个Button按钮,这里就不再赘述,下面看一下我们主控件Activity:
public class Activityone extends Activity {
// 设置通知的ID
private static final int MY_NOTIFICATION_ID = 1;
// 记录通知的数目
private int mNotificationCount;
// 通知中的文本信息
private final CharSequence tickerText = "你有一条新通知,请查看!";
private final CharSequence contentTitle = "你好";
private final CharSequence contentText = "欢迎你来到河南,来到焦作。";
// Notification Action Elements
private Intent mNotificationIntent;
private PendingIntent mContentIntent;
// 设置收到通知时的响铃
private Uri soundURI = Uri.parse("android.resource://cn.edu.hpu.android.activity_notification/" + R.raw.music);
//震动设置,必须在配置文件中声明震动许可
private long[] mVibratePattern = { 0, 200, 200, 300 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
mNotificationIntent = new Intent(Activityone.this, Activitytwo.class);
mContentIntent = PendingIntent.getActivity(Activityone.this, 0, mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
Button mybutton1 = (Button)findViewById(R.id.buttonone1);
mybutton1.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
// 建立通知
Notification.Builder NB = new Notification.Builder(Activityone.this)
.setTicker(tickerText)
.setSmallIcon(android.R.drawable.stat_sys_warning) //设置系统通知图标
.setAutoCancel(true) //设置通知是否可以取消
.setContentTitle(contentTitle)
.setContentText(contentText + " (" + ++mNotificationCount + ")") //显示用户收到几条信息
.setContentIntent(mContentIntent) //设置跳转的地址
.setSound(soundURI) //设置声音的地址
.setVibrate(mVibratePattern); //设置收到通知时震动
// Pass the Notification to the NotificationManager:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MY_NOTIFICATION_ID, NB.build());
}
});
}
}
注意:cn.edu.hpu.android.activity_notification:是我们的主Activity的包名;R.raw.music:我们设置的提示音乐,这里我们的工程目录下并没有raw文件,需要我们在res下自行创建,然后将我们的音乐文件拷贝之此即可。
第二种:自定义通知栏提示视图
既然是自定义,那么我们的自定义视图如下(custom_notification.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7777"
android:padding="3dp" > <ImageView
android:id="@+id/image"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginRight="10dp"
android:contentDescription="Eye"
android:src="@drawable/fire_eye_alien" /> <TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:textSize="10sp" /> </LinearLayout>
同样我们主Activity并没有视图文件,那么我们主Activity代码与上面有哪些区别呢:
public class Activitythree extends Activity {
// Notification ID to allow for future updates
private static final int MY_NOTIFICATION_ID = 2;
// Notification Count
private int mNotificationCount;//记录通知的数目
// Notification Text Elements
private final CharSequence tickerText = "你有一条新通知,请查看!";
private final CharSequence contentTitle = "你好";
private final CharSequence contentText = "欢迎你来到河南,来到焦作。";
// Notification Action Elements
private Intent mNotificationIntent;
private PendingIntent mContentIntent;
// Notification Sound and Vibration on Arrival
private Uri soundURI = Uri.parse("android.resource://cn.edu.hpu.android.activity_notification/"+ R.raw.music);
//震动设置,必须在配置文件中声明震动许可
private long[] mVibratePattern = { 0, 200, 200, 300 };
RemoteViews mContentView = new RemoteViews("cn.edu.hpu.android.activity_notification", R.layout.custom_notification);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
mNotificationIntent = new Intent(Activitythree.this, Activityfour.class);
mContentIntent = PendingIntent.getActivity(Activitythree.this, 0, mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
Button mybutton1 = (Button)findViewById(R.id.buttonone1);
mybutton1.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
mContentView.setTextViewText(R.id.text, contentText + " ("
+ ++mNotificationCount + ")");//设置视图中textview的内容
Notification.Builder NB = new Notification.Builder(Activitythree.this)
.setTicker(tickerText)
.setSmallIcon(android.R.drawable.stat_sys_warning)
.setAutoCancel(true)
.setContent(mContentView)
.setContentIntent(mContentIntent)
.setSound(soundURI)
.setVibrate(mVibratePattern);
// Pass the Notification to the NotificationManager:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MY_NOTIFICATION_ID, NB.build());
}
});
}
}
最后补充一点,这里我们使用到了消息震动提示,所以我们需要在AndroidManifest.xml文件中声明一下震动权限:
<!-- 震动许可声明 -->
<uses-permission android:name="android.permission.VIBRATE" />
到这里关于Android应用消息通知,就为大家介绍完毕,感兴趣的小同鞋可以实现一下,代码简单,如有疑问欢迎留言讨论。新手学习,高手交流。
Android之OptionsMenu与Notification的实现的更多相关文章
- Android新旧版本Notification
Android新旧版本Notification 在notification.setLatestEventInfo() 过时了 以前: NotificationManager mn = (Notific ...
- [Android Tips] 9. framework notification layout font size
android 4.4 framework notification layout 相关字体大小 * title: notification_title_text_size: 18dp * conte ...
- Android消息通知(notification)和PendingIntent传值
通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...
- 第13讲- Android之消息提示Notification
第13讲 Android之消息提示Notification .Notification Notification可以理解为通知的意思一般用来显示广播信息,通知可以显示到系统的上方的状态栏(status ...
- Android 提醒公共方法 Notification
SimpAndroidFarme是近期脑子突然发热想做的android快速开发的框架,目标是模块化 常用的控件,方便新手学习和使用.也欢迎老鸟来一起充实项目:项目地址 今天的目标是做一个公共的提醒方法 ...
- Android控件之Notification
Android通知就是让设备在屏幕最顶上那栏里面显示图标,当滑下通知栏之后可以看到列表状的通知选项,有些是"通知"类型的,有些是"正在运行"类型的," ...
- Android开发4: Notification编程基础、Broadcast的使用及其静态注册、动态注册方式
前言 啦啦啦~(博主每次开篇都要卖个萌,大家是不是都厌倦了呢~) 本篇博文希望帮助大家掌握 Broadcast 编程基础,实现动态注册 Broadcast 和静态注册 Broadcast 的方式以及学 ...
- android:使用RemoteView自定义Notification
//网上相关内容较少,遂记录下来,备忘. //依然以音乐播放器demo为例. 效果截图 //锤子手机上的效果 step1 准备自定义layout 常规的实现方式,并不会因为是用于notificatio ...
- 【Android】监听Notification被清除
前言 一般非常驻的Notification是可以被用户清除的,如果能监听被清除的事件就可以做一些事情,比如推送重新计数的问题. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www ...
随机推荐
- 关于 IE6、 IE7兼容性总结(转)
其实浏览器的不兼容,我们往往是各个浏览器对于一些标准的定义不一致导致的,因此,我们可以进行一些初始化,很多问题都很轻松解决. 下面是14条特殊情况仅供参考: 1. 文字本身的大小不兼容.同样是font ...
- linux下tomcat安装
1.先安装jdk,我们这里用yum进行安装: yum -y install java-1.7.0-openjdk* 确定是否安装成功: java -version 如果显示jdk的版本信息,说明安装成 ...
- ESLint 规则
ESLint由 JavaScript 红宝书 作者 Nicholas C.Zakas 编写, 2013 年发布第一个版本. ESLint是一个以可扩展.每条规则独立的,被设计为完全可配置的lint工具 ...
- JVM垃圾收集器
JVM中垃圾的回收由垃圾收集器进行,随着JDK的不断升级,垃圾收集器也开发出了各种版本,垃圾收集器不断优化的动力,就是为了实现更短的停顿. 下面是7种不同的分代收集器,如果两个收集器之间有连线,则表示 ...
- 转:CentOS, 找不到dump命令:command not found
dump 功能说明:备份文件系统.语 法:dump [-cnu][-0123456789][-b <区块大小>][-B <区块数目>][-d <密度>][-f &l ...
- php面向对象中的几个基本定义
面向对象: 面向对象是现代编程中的一种重要设计方法,其基本思想是使用对象,类,封装,继承等来进行程序设计. 对象: 一只猪,一只羊,一辆汽车. 类: 类的主要作用就是创建对象. 封装: 类的特点是将对 ...
- 完善ecshop的mysql类
前篇文章中,我提及到了如何<提取ecshop的mysql类>.但是没有数据库前缀的写法 废话不说,上步骤(目录结构请参考提取ecshop的mysql类) 修改connfig.php为 &l ...
- StartSSL免费SSL证书申请和账户注册完整过程
StartSSL算是比较早提供免费SSL证书的第三方提供商,我们可以免费申请且免费续期使用到有需要HTTPS网址的用户.关于网站使用SSL证书主要还是因为谷歌在向导说明中提到如果一个网站使用到SSL证 ...
- VB.Net 2010中 ./和../的含义
文件路径 文件路径就是文件在电脑(服务器)中的位置,表示文件路径的方式有两种:相对路径和绝对路径. Windows由于使用 斜杆/ 作为DOS命令提示符的参数标志了,为了不混淆,所以采用 反斜杠\ 作 ...
- linux内核分析作业8:理解进程调度时机跟踪分析进程调度与进程切换的过程
1. 实验目的 选择一个系统调用(13号系统调用time除外),系统调用列表,使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 分析汇编代码调用系统调用的工作过程,特别是参数的传递的方 ...