notification..消息提醒,有震动提醒效果

来源:互联网 发布:迈普数据王锐睿 编辑:程序博客网 时间:2024/05/02 09:18

-------------------main.java---------------------


package org.crazyit.notification;


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.view.View.OnClickListener;
import android.widget.Button;


/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class NotificationTest extends Activity
{
static final int NOTIFICATION_ID = 0x1123;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取应用界面中的Button对象
Button bn = (Button) findViewById(R.id.bn);
//为按钮的单击事件绑定事件监听器
bn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View source)
{
//创建一个启动其他Activity的Intent
Intent intent = new Intent(NotificationTest.this
, OtherActivity.class);
PendingIntent pi = PendingIntent.getActivity(NotificationTest.this
, 0, intent , 0);
//创建一个Notification
Notification notify = new Notification();
//为Notification设置图标,该图标显示在状态栏
notify.icon = R.drawable.notify;
//为Notification设置文本内容,该文本会显示在状态栏
notify.tickerText = "启动其他Activity的通知";
//为Notification设置发送时间
notify.when = System.currentTimeMillis();
//为Notification设置声音
notify.defaults = Notification.DEFAULT_SOUND;
//为Notification设置默认声音、默认振动、默认闪光灯
notify.defaults = Notification.DEFAULT_ALL;
//设置事件信息
notify.setLatestEventInfo(NotificationTest.this, "普通通知",
"点击查看", pi);
//获取系统的NotificationManager服务
NotificationManager notificationManager = (NotificationManager) 
getSystemService(NOTIFICATION_SERVICE);
//发送通知
notificationManager.notify(NOTIFICATION_ID, notify);
}
});
Button del = (Button)findViewById(R.id.del);
del.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//获取系统的NotificationManager服务
NotificationManager notificationManager = (NotificationManager) 
getSystemService(NOTIFICATION_SERVICE);
//取消通知
notificationManager.cancel(NOTIFICATION_ID);
}
});
}
}


----------------------------------------other.java----------------------------


/**
 * 
 */
package org.crazyit.notification;


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


/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class OtherActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//设置该Activity显示的页面
setContentView(R.layout.other);
}
}



--------------------------------------------------main.xml--------------------------------------------------


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/bn"  
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="添加Notification"
/>
<Button
android:id="@+id/del"  
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="删除Notification"
/>
</LinearLayout>



-----------------------------------other.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"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<!-- 定义一个ImageView -->
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/classic"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>


记得加权限:

<!-- 添加操作闪光灯的权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<!-- 添加操作振动器的权限 -->
<uses-permission android:name="android.permission.VIBRATE"/>



,,,,,,,,可以左右移除该notification,,,,,,,,


0 0
原创粉丝点击