zoukankan      html  css  js  c++  java
  • Android 中Notification和NotificationManager的使用

    Notification使用基本步骤:

    Nofification NotificationManager的使用

    1.       获得系统级的服务NofifiactionManager

    String Service = NOFICATION_SERVICE

    NotificationManager nm = (NotificationManager)getSystemService(Service);

    2.       实例化Notificatoin对象,并设置其属性:

     

    Notification n = new Nofification();

    //设置显示图标,会在状态栏显示

    Int icon = R.drawable.icon;

    //设置提示信息,会在状态栏显示

    String tickerText = “Test NotificationManager”;

    //显示时间

    Long when = system.currentTimeMills();

    n.icon = icon;

    n.ticker = tickerText

    n.when = when

    还可以用构造方法来设置

    NotificationManager nm = new NotificationManager(icon,ticker,when);

    3.       调用 setLatestEventInfo()方法在视图中设置图标和时间

    //实例化Intent

    Intent intent = new Intent(this,MainActivity.class);

    //获得PendingIntent

    PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);

    //设置事件信息

    n.setLastestEventInfo(this,”my title”,”my Content”,pi);

    4.  发出通知

    .//标示通知ID

    Int ID = 1;

    Nm.notify(ID,n); 

    实例:在一个activity中去注册一个广播,然后用这个广播去展示这个Notification.:

    package com.king.android.controls;

    import android.app.Activity;
    import android.app.Service;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    import com.king.android.R;

    /**

     * 描述:发起一个广播
     * 作者:Andy.Liu
     * 时间: 2012-7-19  上午08:31:49
     *
    */
    public class NotifityActiivty extends Activity {
        private static final String MY_SERVICE = "com.king.android.controls.MY_NOTIFITY_SERVICE";//指定的通知动作
        
    //声明IPerson接口
        private IPerson iPerson;
        private Button btn;
        MyReceiver2 receiver = new MyReceiver2();

        @Override
        protected void onResume() {
            super.onResume();
            IntentFilter filter = new IntentFilter();
            //实例化MYReceiver2
            registerReceiver(receiver, filter);
        }
        @Override
        protected void onPause() {
            super.onPause();
            unregisterReceiver(receiver);
        }
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            btn = (Button) findViewById(R.id.btn_voice);
            btn.setText("发出通知");
            btn.setVisibility(View.VISIBLE);
            btn.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setAction(MY_SERVICE);
                    sendBroadcast(intent);
                }
            });
        }
        
    }


    package com.king.android.controls;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;

    /**

     * 描述:广播接受通知并发起通知事件
     * 作者:Andy.Liu
     * 时间: 2012-7-19  上午08:34:20
     *
    */
    public class NotifityReciver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Intent i = new Intent();
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setClass(context, DisplayActivity.class);
            context.startActivity(i);
        }

    }



    package com.king.android.controls;

    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;
    import android.widget.TextView;

    import com.king.android.R;

    /**

     * 描述:显示通知
     * 作者:Andy.Liu
     * 时间: 2012-7-19  上午08:36:31
     *
    */
    public class DisplayActivity extends Activity {
        public static final int NOFITY_ID =100;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            
            super.onCreate(savedInstanceState);
            String Service = NOTIFICATION_SERVICE;
            final NotificationManager nm = (NotificationManager)getSystemService(Service);
            setContentView(R.layout.main);
            
            TextView tv = (TextView) findViewById(R.id.tv_info);
            tv.setVisibility(View.VISIBLE);
            Button btnCancel = (Button) findViewById(R.id.btn_voice);
            btnCancel.setVisibility(View.VISIBLE);
            btnCancel.setText("取消广播");
            btnCancel.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    nm.cancel(NOFITY_ID);
                }
            });
        
            Notification n = new Notification();
            int icon = R.drawable.ic_launcher;
            String tickerText = "Test notifiManager";
            long when = System.currentTimeMillis();
            n.icon = icon;
            n.tickerText = tickerText;
            n.when = when;
            //实例化Intent
            Intent intent = new Intent(this,MainActivity.class);
            //获得PendingIntent
            PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
            //设置事件信息
            n.setLatestEventInfo(this,"My title","My content",pi);
            nm.notify(NOFITY_ID,n);

        }

  • 相关阅读:
    ABP记录被删除调用Repository.Get报错
    C# 中在对象后面跟“?” 以及在类型后面跟问号
    list转table
    ling groupby多字段分组统计
    linq一堆多再对多
    ABP下mvc的libs还原
    EF数据迁移
    ABP密码规则设置
    ABP中table时间格式化
    ABP读取appseting.json
  • 原文地址:https://www.cnblogs.com/liuzenglong/p/2598622.html
Copyright © 2011-2022 走看看