Notification管理器

Java代码:

public class main extends Activity {
    /** Called when the activity is first created. */
	int notification_id=19172439;
	NotificationManager nm;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Button bt1=(Button)findViewById(R.id.bt1);
        bt1.setOnClickListener(bt1lis);
        Button bt2=(Button)findViewById(R.id.bt2);
        bt2.setOnClickListener(bt2lis);
        
    }
    OnClickListener bt1lis=new OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showNotification(R.drawable.home,"图标边的文字","标题","内容");
		}
    	
    };
    OnClickListener bt2lis=new OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//showNotification(R.drawable.home,"图标边的文字","标题","内容");
			nm.cancel(notification_id);
		}
    	
    };
    public void showNotification(int icon,String tickertext,String title,String content){
    	//设置一个唯一的ID,随便设置
    	
    	//Notification管理器
    	Notification notification=new Notification(icon,tickertext,System.currentTimeMillis());
    	//后面的参数分别是显示在顶部通知栏的小图标,小图标旁的文字(短暂显示,自动消失)系统当前时间(不明白这个有什么用)
    	notification.defaults=Notification.DEFAULT_ALL; 
    	//这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND
    	//振动为Notification.DEFAULT_VIBRATE;
    	//Light为Notification.DEFAULT_LIGHTS,在我的Milestone上好像没什么反应
    	//全部为Notification.DEFAULT_ALL
    	//如果是振动或者全部,必须在AndroidManifest.xml加入振动权限
    	PendingIntent pt=PendingIntent.getActivity(this, 0, new Intent(this,main.class), 0);
    	//点击通知后的动作,这里是转回main 这个Acticity
    	notification.setLatestEventInfo(this,title,content,pt);
    	nm.notify(notification_id, notification);
    	
    }
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:id="@+id/bt1"
	android:layout_height="wrap_content"
	android:layout_width="fill_parent"
	android:text="Notification测试"
/>
<Button android:id="@+id/bt2"
	android:layout_height="wrap_content"
	android:layout_width="fill_parent"
	android:text="清除Notification"
/>
</LinearLayout>

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值