zoukankan      html  css  js  c++  java
  • 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;
     }
     
    }

  • 相关阅读:
    非诚勿扰骆琦攻略
    IT服务者的困惑与解决之道
    某某银行IT运维管理的三点和四化
    提升CIO地位及IT价值体现,IT治理理念在中国势在必行
    振兴民族软件,险恶的江湖该如何仗剑走天涯
    某连锁饭店IT服务台、自助服务建设
    证券行业ITIL初探助力券商成就IT管理之路
    分享屡见成效的另类方法论保障ITIL软件及ITSM方案落地实施
    城市商业银行IT科技工作管理之痛
    【转】陈天晴:信息化发展要注意规划调整 重视IT治理
  • 原文地址:https://www.cnblogs.com/honeynm/p/4287003.html
Copyright © 2011-2022 走看看