zoukankan      html  css  js  c++  java
  • android笔记:Notification通知的使用

    通知(Notification,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现。

    发出一条通知后,手机最上方的状态栏中会显示一个通知的图标,下拉状态栏后可以看到通知的详细内容。

    《第一行代码》中的Notification的构造方法、setLatestEventInfo等方法已经过时了,但是思想还是一致的。

    最新的具体做法如下:

    1.需要一个 NotificationManager 来对通知进行管理,可以调用Context getSystemService(NOTIFICATION_SERVICE)方法获取到;

    2.创建一个 Notification 对象,可通过NotificationCompat的Builder方法来实现.

    setTicker()用来显示通知在状态栏的提示信息,

    setSmallIcon()指定通知的图标

    setContentTitle指定通知的标题,setContentText指定通知内容

    setContentIntent指定跳转界面的PendingIntent,PendingIntent 简单地理解为延迟执行的 Intent

    3. notify()方法就可以让通知显示出来了。

    notify()方法接收两个参数,第一个参数是 id,要保证为每个通知所指定的 id 都是
    不同的。第二个参数则是 Notification 对象,这里直接将我们刚刚创建好的 Notification 对象
    传入即可。

     

    具体代码如下:

    MainActivity:

    package com.example.notificationtest;

    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.support.v4.app.NotificationCompat;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class MainActivity extends Activity implements OnClickListener {

        private Button sendNotice;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            sendNotice = (Button) findViewById(R.id.send_notice);
            sendNotice.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.send_notice:
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        
                Intent intent = new Intent(this, NotificationActivity.class);
                PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
                        PendingIntent.FLAG_CANCEL_CURRENT);
                
                Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setTicker("This is ticker text")
                .setContentTitle("This is title")
                .setAutoCancel(true)
                .setContentText("This is content text")
                .setContentIntent(pi)    
                .build();
                
                manager.notify(1, notification); 
                break;
            default:
                break;
            }
        }

    }


    NotificationActivity:

    package com.example.notificationtest;
    
    import android.app.Activity;
    import android.app.NotificationManager;
    import android.os.Bundle;
    
    public class NotificationActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.notification_layout);
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            manager.cancel(1);
        }
    
    }

    activity_main.xml:

    <LinearLayout 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:orientation="vertical" >
    
        <Button 
            android:id="@+id/send_notice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Send notice"
            />
        
    </LinearLayout>

    notification_layout.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:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="24sp"
            android:text="This is notification layout"
            />
        
    </RelativeLayout>

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.notificationtest"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.notificationtest.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".NotificationActivity" >
            </activity>
    
        </application>
    
    </manifest>
  • 相关阅读:
    UVA——A Spy in the Metro(线性dp)
    IDEA运行jsp文件变成源码详细解决方案
    CF1105C Ayoub and Lost Array(dp+矩阵快速幂优化)
    牛客练习赛75——A广义肥波
    void * 指针和const 指针
    详解getchar()函数与缓冲区
    深入了解scanf() getchar()和gets()等函数之间的区别
    字符串和指针注意
    指针
    数组和字符串的小结
  • 原文地址:https://www.cnblogs.com/expiator/p/5960574.html
Copyright © 2011-2022 走看看