zoukankan      html  css  js  c++  java
  • 消息提醒,初探Notification

    1:MainActivity.java

    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 {
        private static  int NOTIFICATION_ID = 0;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            Button btnSendNotification = (Button)findViewById(R.id.btnSendNotification);
            
            btnSendNotification.setOnClickListener(btnOnClickListener());
        }
        
        OnClickListener btnOnClickListener(){
            return new OnClickListener(){
    
                @Override
                public void onClick(View arg0) {
                    switch(arg0.getId()){
                    case R.id.btnSendNotification:
                        Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
                        String title = "消息标题";
                        intent.putExtra("title", title);
                        
                        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
                        
                        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                        Notification n = new NotificationCompat.Builder(MainActivity.this)
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setTicker("消息提示").setContentTitle(title)
                                .setContentText("消息子标题")
                                .setContentInfo("消息内容")
                                .setWhen(System.currentTimeMillis())        //显示当前时间
                                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)    //使用默认的声音与震动
                                .setContentIntent(pi)
                                .setAutoCancel(true)
                                .build();
    
                        nm.notify(++NOTIFICATION_ID, n);
                        break;
                    }
                }
            };
        }
    
    }

    2:activity_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="com.example.notificationtest.MainActivity$PlaceholderFragment" >
    
        <Button
            android:id="@+id/btnSendNotification"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/send_notification" />
        
    </RelativeLayout>

    3:NotificationActivity.java

    package com.example.notificationtest;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class NotificationActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            
            setContentView(R.layout.activity_notification);
            
            TextView tvInfo = (TextView)findViewById(R.id.tvInfo);
            tvInfo.setText(getIntent().getStringExtra("title"));
            
        }
    }

    4:activity_notification.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:gravity="center">
        
        <TextView 
            android:id="@+id/tvInfo"
            android:textSize="22sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Notificaiton Info"/>
    </LinearLayout>
  • 相关阅读:
    three.js raycaster射线碰撞的坑 (当canvas大小 不是屏幕大小是解决拾取物体的办法)
    如何去掉IE文本框后的那个X css代码
    解决input 有readonly属性 各个浏览器的光标兼容性问题
    centos的基本命令03(du 查看文件详情,echo清空文件内容)
    centos的 / ~
    centos的基本命令02
    centos的基本命令01
    关系性数据库和非关系型数据库
    绝对路径和相对路径的理解
    linux的目录和基本的操作命令
  • 原文地址:https://www.cnblogs.com/yshyee/p/4049864.html
Copyright © 2011-2022 走看看