• Notification


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".Notice" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

    </RelativeLayout>

    main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".Main" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="65dp"
            android:text="Notification" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_below="@+id/button1"
            android:layout_marginTop="38dp"
            android:text="clear Notification" />

    </RelativeLayout>

    Main.java

    package com.example.mynotification;

    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;

    public class Main extends Activity {
     
     Button   btn,btn2;
     
     NotificationManager mNotificationManager;
     
     Notification  mNotification;
     
     Intent intent;
     
     PendingIntent pIntent;
     
     final static int ID=1;
     
     @Override
     protected void onCreate(Bundle savedInstanceState) {
     
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      
      setTitle("消息通知");
      
      mNotificationManager = (NotificationManager) this
        .getSystemService(NOTIFICATION_SERVICE);
      
      mNotification = new Notification();
      
      mNotification.icon = R.drawable.ic_launcher;
      
      mNotification.tickerText = "显示在状态栏";
      
      long when = System.currentTimeMillis();
      
      mNotification.when = when;// 显示的时间
      
      mNotification.sound = Uri.parse("android.resource://com.sun.alex/raw/dida"); // 自定义声音
      
      /*

      * 添加声音

      * notification.defaults |=Notification.DEFAULT_SOUND;

      * 或者使用以下几种方式

      * notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

      * notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

      * 如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"FLAG_INSISTENT"

      * 如果notification的defaults字段包括了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音

      */
      
      /*

      * 添加振动

      * notification.defaults |= Notification.DEFAULT_VIBRATE;

      * 或者可以定义自己的振动模式:

      * long[] vibrate = {0,100,200,300}; //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒

      * notification.vibrate = vibrate;

      * long数组可以定义成想要的任何长度

      * 如果notification的defaults字段包括了"DEFAULT_VIBRATE",则这个属性将覆盖vibrate字段中定义的振动

      */
      
      /*

      * 添加LED灯提醒

      * notification.defaults |= Notification.DEFAULT_LIGHTS;

      * 或者可以自己的LED提醒模式:

      * notification.ledARGB = 0xff00ff00;

      * notification.ledOnMS = 300; //亮的时间

      * notification.ledOffMS = 1000; //灭的时间

      * notification.flags |= Notification.FLAG_SHOW_LIGHTS;

      */


      
      mNotification.flags = Notification.FLAG_NO_CLEAR;
      
      //mNotification.defaults = Notification.DEFAULT_ALL;
      
      btn = (Button) findViewById(R.id.button1);
      
      btn.setOnClickListener(new View.OnClickListener() {
       
       @SuppressWarnings("deprecation")
       @Override
       public void onClick(View v) {
        
        intent = new Intent(Main.this,Notice.class);
        
        pIntent = PendingIntent.getActivity(Main.this, 0, intent, 0);
        
        mNotification.setLatestEventInfo(Main.this, "我的通知", "小小的測試", pIntent);
        //getApplicationContext()
        mNotificationManager.notify(ID, mNotification);
        
       
       }
      });
      
      btn2 = (Button) findViewById(R.id.button2);
      
      btn2.setOnClickListener(new View.OnClickListener() {
       
       @Override
       public void onClick(View v) {
        
        mNotificationManager.cancel(ID);
       
       }
      });
     }
     
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
     
      // Inflate the menu; this adds items to the action bar if it is
      // present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
     }
     
    }

    notice.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".Notice" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

    </RelativeLayout>

    Notice.java


    package com.example.mynotification;

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

    public class Notice extends Activity {
     
     @Override
     protected void onCreate(Bundle savedInstanceState) {
     
      super.onCreate(savedInstanceState);
      setContentView(R.layout.notice);
     }
     
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
     
      // Inflate the menu; this adds items to the action bar if it is
      // present.
      getMenuInflater().inflate(R.menu.notice, menu);
      return true;
     }
     
    }

  • 相关阅读:
    基于Canal和Kafka实现MySQL的Binlog近实时同步
    Levenshtein Distance(编辑距离)算法与使用场景
    超强图文|并发编程【等待/通知机制】就是这个feel~
    volatile和synchronized到底啥区别?多图文讲解告诉你
    读《Clean Code 代码整洁之道》之感悟
    小小TODO标识,你用对了吗?
    深入理解JVM(③)判断对象是否还健在?
    深入理解JVM(③)虚拟机的类加载器(双亲委派模型)
    深入理解JVM(③)经典的垃圾收集器
    深入理解JVM(③)HotSpot虚拟机对象探秘
  • 原文地址:https://www.cnblogs.com/honeynm/p/4287003.html
Copyright © 2020-2023  润新知