zoukankan      html  css  js  c++  java
  • Android笔记之Notification使用

    1、普通Notification和自定义布局的Notification:

    (1)如果不是在activity显示通知,但有传context,则
     manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    package com.example.mynotification;
    
    import android.app.Activity;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.RemoteViews;
    
    public class MainActivity extends Activity {
    
        private Button button,button2;
        //别忘了实例化NotificationManager,Notification.Builder 
        //二者通过Notification notification=builder.build();以及
        //NotificationManager.notify(int id, Notification notification)
        private NotificationManager manager;
        Notification.Builder builder;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button = (Button) findViewById(R.id.button1);
            button2 = (Button) findViewById(R.id.button2);
            //实例化NotificationManager
            manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            //实例化Builder
            builder=new Notification.Builder(MainActivity.this);
            button.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    // Notification.Builde->Notification->NotificationManager
                    // 图标,标题,消息,PenddIngntent
                    Intent intent = new Intent(MainActivity.this,
                            MainActivity.class);
                    PendingIntent pendingIntent = PendingIntent.getActivity(
                            MainActivity.this, 0, intent, 0);
                    //点击通知跳转的pendingIntent
                    builder.setContentIntent(pendingIntent);
                    //通知的标题
                    builder.setContentTitle("标题");
                    //通知的正文文本
                    builder.setContentText("你好啊!");
                    //状态栏上的图标以及拉下之后左侧的图标,必不可少
                    builder.setSmallIcon(R.drawable.noticon);
                    //第一次产生提醒时会显示的文本
                    builder.setTicker("普通通知");
                    //设置消息提醒默认的设置
                    //默认震动;震动需要添加用户权限
                    //默认闪光
                    //默认铃声
                    builder.setDefaults(Notification.DEFAULT_ALL);
                    Notification notification=builder.build();
                    //设置使点击后通知消失
                    notification.flags=Notification.FLAG_AUTO_CANCEL;
                    // 1000为id数值,唯一标识
                    manager.notify(1, notification);
                }
            });
            button2.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Notification.Builder builder=new Notification.Builder(MainActivity.this);
                    RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.mynoti);
                    remoteViews.setTextViewText(R.id.textView1, "自定义通知!");
                    Intent intent = new Intent(MainActivity.this,
                            MainActivity.class);
                    PendingIntent pendingIntent = PendingIntent.getActivity(
                            MainActivity.this, 0, intent, 0);
                    //指定Intent
                    builder.setContentIntent(pendingIntent);
                    //自定义布局
                    builder.setContent(remoteViews);
                    //设置图标必不可少
                    builder.setSmallIcon(R.drawable.noticon);
                    builder.setTicker("自定义通知");
                    Notification notification=builder.build();
                    manager.notify(5, notification);
                }
            });
        }
    
    }

    自定义的布局:mynoti.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/button1"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="TextView"
            android:textColor="#669966"
            android:textSize="25dp" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="28dp"
            android:text="开始" />
    
    </RelativeLayout>

    Done!

  • 相关阅读:
    从原理上理解NodeJS的适用场景
    core 基本操作
    SQL Server 触发器
    Centos 7 Apache .netcore 做转发
    Windows Server 使用 WAMP 并配置 Https
    centos7 apache php git pull
    Visual StudioTools for Unity 使用技巧2
    如何实现Windows Phone代码与Unity相互通信(直接调用)
    关于NGUI与原生2D混用相互遮盖的问题心得
    关于NGUI制作图集在低内存设备上的注意事项
  • 原文地址:https://www.cnblogs.com/xingyyy/p/3389679.html
Copyright © 2011-2022 走看看