• 安卓学习第33课——notification


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        >
    <Button
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="发送Notification"
        android:onClick="send"
        />
    <Button
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="删除Notification"
        android:onClick="del"
        />    
    </LinearLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        >
    <!-- 定义一个ImageView -->
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/java"
        android:layout_gravity="center_horizontal"
        />
    </LinearLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.notification"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="16"
            android:targetSdkVersion="19" />
    
        <!-- 添加操作闪光灯的权限 -->
        <uses-permission android:name="android.permission.FLASHLIGHT" />
        <!-- 添加操作振动器的权限 -->
        <uses-permission android:name="android.permission.VIBRATE" />    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.notification.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=".OtherActivity"
                android:label="@string/other_activity">
            </activity>
        </application>
    
    </manifest>

    当点击notification时,实现otherActivity

    package com.example.notification;
    
    
    import android.app.Activity;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        static final int NOTIFICATION_ID=0x123;
        NotificationManager nm;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);    
        }
        // 为发送通知的按钮的点击事件定义事件处理方法
            public void send(View source)
            {
                // 创建一个启动其他Activity的Intent
                Intent intent = new Intent(MainActivity.this
                    , OtherActivity.class);
                PendingIntent pi = PendingIntent.getActivity(
                        MainActivity.this, 0, intent, 0);
                Notification notify = new Notification.Builder(this)
                    // 设置打开该通知,该通知自动消失
                    .setAutoCancel(true)
                    // 设置显示在状态栏的通知提示信息
                    .setTicker("有新消息")
                    // 设置通知的图标
                    .setSmallIcon(R.drawable.notify)
                    // 设置通知内容的标题
                    .setContentTitle("一条新通知")
                    // 设置通知内容
                    .setContentText("恭喜你,您加薪了,工资增加20%!")
                    // // 设置使用系统默认的声音、默认LED灯
                    // .setDefaults(Notification.DEFAULT_SOUND
                    // |Notification.DEFAULT_LIGHTS)
                    // 设置通知的自定义声音
                    .setSound(Uri.parse("android.resource://com.example.notification/"
                        + R.raw.mylove1))
                    .setWhen(System.currentTimeMillis())
                    // 设改通知将要启动程序的Intent
                    .setContentIntent(pi).build();
                // 发送通知
                nm.notify(NOTIFICATION_ID, notify);
            }
    
            // 为删除通知的按钮的点击事件定义事件处理方法
            public void del(View v)
            {
                // 取消通知
                nm.cancel(NOTIFICATION_ID);
            }
        
    }
    package com.example.notification;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class OtherActivity extends Activity
    {
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            //设置该Activity显示的页面
            setContentView(R.layout.other);
        }
    }

  • 相关阅读:
    preliminary->advanced exam selections
    Maven入门
    Ajax和Json
    过滤器和监听器
    JSTL标签库
    JSP与EL表达式
    dom4j与XML文档操作
    会话管理
    登录之验证码
    WEB之文件下载
  • 原文地址:https://www.cnblogs.com/Yvettey-me/p/3970152.html
Copyright © 2020-2023  润新知