首页
JAVA笔记
数据库笔记
混乱记忆
小站记
您现在的位置
android Notification 使用指南
简介: android Notification 简单使用、自定义、取消

简单使用用

package org.xiaoyunduo.notification;
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.LinearLayout;
import android.widget.RemoteViews;

public class MainActivity extends Activity {

 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  LinearLayout layout = new LinearLayout(this);
  Button create  = new Button(this);
  create.setText("发送通知");
  layout.addView(create);
  Button destroy  = new Button(this);
  destroy.setText("取消通知");
  layout.addView(destroy);
  this.setContentView(layout);
  
  create.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
     NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
     Notification notfi = new Notification(R.drawable.ic_launcher, "有一个通知", System.currentTimeMillis());
     PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0,new Intent(MainActivity.this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
     notfi.setLatestEventInfo(MainActivity.this, "标题", "小云朵", contentIntent);
     nm.notify(123321,notfi);
   }
  });
  
  
  destroy.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
     NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
     nm.cancel(123321);
   }
  });
 }
}

 

通知自定义

 

  1. 配置 布局XML 命名为 a.xml
    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:baselineAligned="false"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
     <ImageView android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dip" />
        <TextView android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffffff" />
     <ProgressBar android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:max="100"
            android:progress="50"
            android:secondaryProgress="75" />
    </LinearLayout>
  2. 实现代码
    package org.xiaoyunduo.notification;
    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.LinearLayout;
    import android.widget.RemoteViews;
    public class MainActivity extends Activity {
     
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
    LinearLayout layout = new LinearLayout(this);
      Button create  = new Button(this);
      create.setText("发送通知");
      layout.addView(create);
      Button destroy  = new Button(this);
      destroy.setText("取消通知");
      layout.addView(destroy);
      this.setContentView(layout);
      
      create.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
         NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
         Notification notfi = new Notification(R.drawable.ic_launcher, "有一个通知", System.currentTimeMillis());
         PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0,new Intent(MainActivity.this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.a);
         contentView.setTextViewText(R.id.text, "小云朵");
         contentView.setImageViewResource(R.id.icon, R.drawable.ic_launcher);
         notfi.contentIntent =contentIntent;
         notfi.contentView = contentView;

        
         nm.notify(123321,notfi);
       }
      });
      
      
      destroy.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
         NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
         nm.cancel(123321);
       }
      });
     }
    }

Notification 的使用就先到这里,部分细节参数以后用到再补充进来

    1. notification 创建时提示声音和振动的方法:
      注:下面的代码在使用RemoteViews时无效
      notification.defaults = Notification.DEFAULT_ALL;
      添加权限:<uses-permission android:name="android.permission.VIBRATE" />