Android Notification 例子

package com.test;

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.widget.Button;

public class AndroidtestActivity extends Activity {
	
	 NotificationManager mManager = null;
	    Notification notification =null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // 创建Notification对象 参数分别代表 通知栏 中显示的图标 显示的标题 显示的时间
        notification = new Notification(R.drawable.ic_launcher,
                "Android专业开发群", System.currentTimeMillis());
        
        // 设置在通知栏中点击后Notification自动消失
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        
        //设置点击后转跳的新activity
        Intent intent = new Intent(this, TestA.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);

        //通过bundle可以带一些数据过去 这里将字符串传递了过去
        Bundle bundle = new Bundle();
        bundle.putString("name", "从Notification转跳过来的");
        intent.putExtras(bundle);
        
        //设置通知栏中显示的内容
        PendingIntent contentIntent = PendingIntent.getActivity(this,
                R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(this, "Android专业开发群",
                "QQ群号 164257885", contentIntent);
        
        
        Button button0 = (Button)findViewById(R.id.button1);
        button0.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                //打开这个Notification通知
                mManager.notify(0, notification);
            }
        });
        
        Button button1 = (Button)findViewById(R.id.button2);
        button1.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                //关闭这个Notification通知
                mManager.cancelAll();
            }
        });
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值