本章主要介绍了通知、短信、调用摄像头和相册、播放多媒体文件等内容。  

(一)通知的用法

  1、通知的基本用法

  见如下代码(详细操作步骤在代码注释中):

  (1)先创建一个布局文件,其中只有一个名为“发送通知”的Button,当点击这个按钮的时候发送一条通知:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/sent_notice_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送通知" /> </LinearLayout>

  (2)MainActivity:

 import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button sendNoticeBtn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button sendNoticeBtn = (Button) findViewById(R.id.sent_notice_btn);
sendNoticeBtn.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sent_notice_btn:
// 1.通过getSystemService方法创建NotificationManager实例
NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // 2.创建Notification实例,它是在屏幕上方一闪而过的那种通知信息
// 第一个参数表示图标,第二个参数表示通知内容,第三个参数用于指定通知被创建的时间,当下拉状态时,这个时间会显示在对应的通知上
Notification notification = new Notification(
R.drawable.ic_launcher, "您有一条通知",
System.currentTimeMillis()); // 3.对下拉状态栏后显示的通知的布局进行设定
// 第一个参数表示context上下文,第二个参数表示通知的标题,第三个参数表示通知的内容,第四个参数表示点击通知后的行为,这里先传入null
notification.setLatestEventInfo(this, "这是通知标题", "这是通知内容", null); // 4.使用NotificationManager的notify方法让通知显示出来
// 第一个参数:通知的id,具有唯一性;第二个参数:Notification对象
mgr.notify(1, notification);
break; default:
break;
}
}
}

  2、为通知加上点击跳转功能(使用PendingIntent)

  (1)在以上代码的基础上,再创建另一个xml文件,作为点击通知后跳转到的页面:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是通知跳转的页面" /> </LinearLayout>

  (2)创建活动NotificationActivity,在其中调用NotificationManager的cancel方法让状态栏的通知消失:

 import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle; public class NotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification); // 用于让状态栏上的通知消失,这里的1就是在MainActivity中创建通知时为通知设置的id
NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mgr.cancel(1);
}
}

  (3)修改MainActivity活动的按钮点击事件:

   @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sent_notice_btn:
// 1.通过getSystemService方法创建NotificationManager实例
NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // 2.创建Notification实例,它是在屏幕上方一闪而过的那种通知信息
// 第一个参数表示图标,第二个参数表示通知内容,第三个参数用于指定通知被创建的时间,当下拉状态时,这个时间会显示在对应的通知上
Notification notification = new Notification(
R.drawable.ic_launcher, "您有一条通知",
System.currentTimeMillis()); // 3.对下拉状态栏后显示的通知的布局和点击行为进行设定
// 第一个参数表示context上下文,第二个参数表示通知的标题,第三个参数表示通知的内容,第四个参数表示点击通知后的行为,这里先传入null // 3.1 创建Intent,用于在点击通知时跳转到NotificationActivity页面:
Intent intent = new Intent(MainActivity.this,
NotificationActivity.class);
// 3.2 创建PendingIntent
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT); notification.setLatestEventInfo(this, "这是通知标题", "这是通知内容", pi); // 4.使用NotificationManager的notify方法让通知显示出来
// 第一个参数:通知的id,具有唯一性;第二个参数:Notification对象
mgr.notify(1, notification);
break; default:
break;
}
}

  (4)最后注册NotificationActivity活动。

  3、通知的高级技巧

  (1)在发送通知的时候同时播放一段音频:只用在发送通知按钮的点击事件中加入如下代码:

        ...
       // +.在发送通知的时候播放一段音频,这里的路径是手机默认来电铃声
Uri soundUri = Uri.fromFile(new File(
"/system/media/audio/ringtones/Basic_tone.ogg"));
notification.sound = soundUri; // 4.使用NotificationManager的notify方法让通知显示出来
// 第一个参数:通知的id,具有唯一性;第二个参数:Notification对象
mgr.notify(1, notification);
        ...

   Tips:用R.E管理器进入/system/media/audio,里面有四个文件夹,分别是alarms(闹钟铃声),notifications(通知即短信铃声),ringtones(来电铃声),ui(一些应用程序操作的效果声音比如拍照等)。

  (2)发送通知时让手机振动:

        ...
       // +.发送通知时让手机振动:
// 说明:这个数组下标为0的值表示手机静止的时长(毫秒),下标为1的表示振动的时长,下标为2的表示静止的时长,...以此类推
// 让手机振动需要申请权限:<uses-permission
// android:name="android.permission.VIBRATE" />
long[] vibrates = { 0, 1000, 1000, 1000 };
notification.vibrate = vibrates; // 4.使用NotificationManager的notify方法让通知显示出来
// 第一个参数:通知的id,具有唯一性;第二个参数:Notification对象
mgr.notify(1, notification);
       ...

  (3)控制LED灯亮

             ...
// +.发送通知的时候让LED灯亮
notification.ledARGB = Color.GREEN; // 控制亮灯的颜色,一般可以选红绿蓝三种颜色
notification.ledOnMS = 1000; // 灯亮的时长(毫秒)
notification.ledOffMS = 2000; // 灯暗去的时长,在手机在锁屏状态且用户查看通知之前,灯会交替亮、暗下去
notification.flags = Notification.FLAG_SHOW_LIGHTS; // 指定通知的行为,这里是亮灯
// 4.使用NotificationManager的notify方法让通知显示出来
// 第一个参数:通知的id,具有唯一性;第二个参数:Notification对象
mgr.notify(1, notification);
...

  (4)通知的铃声、振动、灯光的设置使用系统默认值:

             // +.铃声、振动、通知的设置使用系统默认值
notification.defaults = Notification.DEFAULT_ALL;

随机推荐

  1. D - Sigma Function 1~n内有多少个约数和为偶数

    /** 题目:D - Sigma Function 链接:https://vjudge.net/contest/154246#problem/D 题意:求1~n内约数和为偶数的数的个数. 思路:一个数 ...

  2. Spell checker - poj 1035 (hash)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22541   Accepted: 8220 Description Yo ...

  3. thinkphp 跨模块调用

    5.13 跨模块调用   在开发过程中经常会在当前模块调用其他模块的方法,这个时候就涉及到跨模块调用,我们还可以了解到A和R两个快捷方法的使用.例如,我们在Index模块调用User模块的操作方法 c ...

  4. error LNK2019: unresolved external symbol 的一个解决方法

    在VS2010中使用opencv时,有时会出现如下类似的连接错误: 解决方法:根据头文件手动指定lib文件 #ifdef _DEBUG #pragma comment(lib,"*.lib& ...

  5. execute,executeQuery和executeUpdate的区别

    在jdbc中有3种执行sql的语句分别是execute,executeQuery和executeUpdate execute执行增删改查操作 execute返回的结果是个boolean型,当返回的是t ...

  6. Linux vi 文件编辑

    1.通过vi filename 进入编辑状态 2.退出 vi 编辑器 退出命令 说明 q 如果文件未被修改,会直接退回到Shell:否则提示保存文件. q! 强行退出,不保存修改内容. wq w 命令 ...

  7. asp.net网站底部的版权信息实现代码且可维护

    网站底部的版权信息在特殊情况还是比较重要的所以在实现的时候一定要尽可能的做到可维护性,接下来将介绍一些技巧可达到可维护效果,感兴趣的你可不要错过了哈 一个大网站页面很多,如果每个版权信息直接写在下面, ...

  8. SlidingMenu官方实例分析3——PropertiesActivity

    PropertiesActivity此类主要是对SlidingMenu设置的一些展示,也是为了使用者能快速的掌握SlidingMenu 的特点. 首先获得SlidingMenu对象: SlidingM ...

  9. Java快车读书笔记

    办公自动化:OA 客户关系管理:CRM人力资源:HR 企业资源计划:ERP知识管理:KM 供应链管理:SCM企业设备管理系统:EAM 产品生命周期管理:PLM面向服务体系架构:SOA 商业智能:BI项 ...

  10. UVALive 5873 (几何+思维)

    唉 被秀了... 还是太弱,说好的数形结合呢,列个式子出来后就被吓到了,然后就懵逼了. 题意: 有一条狗,从原点出发,沿n个向量走,每个向量只走一次,沿着一个向量(x,y)走时,既可以往(x,y)方向 ...