效果图,此时已经点击,个人设置通知会听从顶端弹出。
MainActivity.java
package com.example.test;
import java.io.File;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn:
Intent intent;
PendingIntent pi;
NotificationManager manager;
Notification notification;
Uri soundUri;
// 创建notification对象来存储通知所需的各种信息,第一个参数为图标,第二个参数用于指定通知的ticker内容
// 第三个参数用于指定通知被创建的时间,以毫秒为单位,以下涉及到时间的均为毫秒单位
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.ic_launcher,
"this is ticker text", System.currentTimeMillis());
/*
* 此处设置点击的activity的跳转 第一个参数依旧是Context 第二个参数一般用不到,所以用0表示取默认值
* 第三个参数就是一个Intent对象
* FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,
* 那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
*/
intent = new Intent(this, NotificationActivity.class);// 定义一个跳转
pi = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);// 将跳转放在延迟跳转中
/*
* 设置通知的布局, 第一个参数为Context,第二个参数用于指定通知的标题,第三个参数用于指定通知的征文内容
* 第四个参数用于传入PendingIntent对象,用于设置点击效果
*/
notification.setLatestEventInfo(this, "this is contenttitle",
"this is cont text", pi);
/*
* 设置通知的声音,取自系统文件“/Emaillcoming.ogg”
* 笔者是自己在手机上随意找的一首歌和位置信息贴上来的,点通知就开始放音乐了,手机来电也是类似的
*/
soundUri = Uri
.fromFile(new File(
"/storage/emulated/0/netease/cloudmusic/Music/Jasmine Thompson - Let Her Go.mp3"));
notification.sound = soundUri;
/*
* 设置振动,vibrate是一个长整型数组,设置手机静止和振动时长, 先静止等待,然后振动,然后静止,然后振动...循环,
* 比如下面表示点击后马上振动一秒,然后静止一秒,然后再振动一秒,后面再没有数值,那么振动活动终止
* 需要自己在manifest里配置权限,代码在下面贴出
*/
// long[] vibrates = { 0, 1000, 1000, 1000 };
// notification.vibrate = vibrates;
/*
* 设置LED指示灯的闪烁 ledARGB设置颜色 ledOnMS指定LED灯亮起的时间 ,ledOffMS指定LED灯暗去的时间
* flags用于指定通知的行为
*/
// notification.ledARGB = Color.GREEN;
// notification.ledOnMS = 1000;
// notification.ledOffMS = 1000;
// notification.flags = Notification.FLAG_SHOW_LIGHTS;
// 如果不想进行繁杂的设置,也可以直接使用通知的默认效果,它会根据当前手机的环境来决定放什么铃声,以及振动,如下
// notification.defaults = Notification.DEFAULT_ALL;
// 第一个参数是id,第二个是notification对象,不同的notification对象设置不同的id
manager.notify(1, notification);
break;
default:
break;
}
}
}
NotificationActivity.java
package com.example.test;
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.notification_layout);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1);//点击后消除通知,1对应前面的1;
}
}
avtivity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="21dp"
android:text="send notification" />
</RelativeLayout>
notification_layout.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:padding="20dp"
android:gravity="center"
android:layout_marginTop="40dp"
android:text="This is the notification layout."
android:textSize="24sp" />
</LinearLayout>
Manifest.xml中声明权限
<uses-permission android:name="android.permission.VIBRATE"/>,声明振动权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NotificationActivity"></activity>
</application>
</manifest>