-
MainActivity.java
-
-
public class MainActivity extends Activity
-
{
-
static final int NOTIFICATION_ID = 0x123;
-
NotificationManager nm;
-
@Override
-
public void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
// 获取系统的NotificationManager服务
-
nm = (NotificationManager)
-
getSystemService(NOTIFICATION_SERVICE);
-
}
-
// 为发送通知的按钮的点击事件定义事件处理方法
-
public void send(View source)
-
{
-
// 创建一个启动其他Activity的Intent
-
Intent intent = new Intent(MainActivity.this
-
, OtherActivity.class);
-
PendingIntent pi = PendingIntent.getActivity(
-
MainActivity.this, 0, intent, 0);
-
Notification notify = new Notification.Builder(this)
-
// 设置打开该通知,该通知自动消失
-
.setAutoCancel(true)
-
// 设置显示在状态栏的通知提示信息
-
.setTicker("有新消息")
-
// 设置通知的图标
-
.setSmallIcon(R.drawable.notify)
-
// 设置通知内容的标题
-
.setContentTitle("一条新通知")
-
// 设置通知内容
-
.setContentText("恭喜你,您加薪了,工资增加20%!")
-
// 设置使用系统默认的声音、默认LED灯
-
// .setDefaults(Notification.DEFAULT_SOUND
-
// |Notification.DEFAULT_LIGHTS)
-
// 设置通知的自定义声音
-
.setSound(Uri.parse("android.resource://org.crazyit.ui/"
-
+ R.raw.msg))
-
.setWhen(System.currentTimeMillis())
-
// 设改通知将要启动程序的Intent
-
.setContentIntent(pi) // ①
-
.build();
-
// 发送通知
-
nm.notify(NOTIFICATION_ID, notify);
-
}
-
// 为删除通知的按钮的点击事件定义事件处理方法
-
public void del(View v)
-
{
-
// 取消通知
-
nm.cancel(NOTIFICATION_ID);
-
}
-
}
-
-
-
OtherActivity
-
-
public class OtherActivity extends Activity
-
{
-
@Override
-
public void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
//设置该Activity显示的页面
-
setContentView(R.layout.other);
-
}
-
}
-
-
XML文件
-
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="horizontal"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:gravity="center_horizontal">
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="发送Notification"
-
android:onClick="send"
-
/>
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="删除Notification"
-
android:onClick="del"
-
/>
-
</LinearLayout>
-
-
other.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="wrap_content"
-
android:gravity="center_horizontal"
-
android:orientation="vertical">
-
<!-- 定义一个ImageView -->
-
<ImageView
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:src="@drawable/swift"
-
android:layout_gravity="center_horizontal"
-
/>
-
</LinearLayout>
-
-
Menu.xml
-
-
<menu xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools="http://schemas.android.com/tools"
-
tools:context=".MainActivity">
-
<item android:id="@+id/action_settings"
-
android:title="@string/app_name"
-
android:orderInCategory="100"
-
android:showAsAction="never"/>
-
</menu>