android Notification 状态栏通知使用示例

Wesley13
• 阅读 604

ch7_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" >
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="这是一个Notification使用示例"/>
    <Button android:id="@+id/bt_sendNotification"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="发送Notification"/>

</LinearLayout>

ch7_second.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" >
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="这是一个通过Notification启动的Activity"/>

</LinearLayout>

NotificationActivity.java:

package com.example.ch7;

import com.example.baseexample.R;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.location.GpsStatus.NmeaListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class NotificationActivity extends Activity {

    private Button bt_sendNotification = null;
    private Intent mIntent = null;
    private PendingIntent mPendingIntent = null;
    private Notification mNotification = null;
    private NotificationManager mNotificationManager = null;
    
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ch7_notification);
        
        bt_sendNotification = (Button)findViewById(R.id.bt_sendNotification);
        bt_sendNotification.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                mIntent = new Intent(NotificationActivity.this,SecondActivity.class);
                mPendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, mIntent, 0);
                mNotification = new Notification();
                mNotification.icon = R.drawable.ic_launcher;
                mNotification.tickerText="实例";
                mNotification.defaults = Notification.DEFAULT_ALL;
                mNotification.flags = Notification.FLAG_INSISTENT;
                mNotification.setLatestEventInfo(NotificationActivity.this, "点击查看", "这是一个Notification示例", mPendingIntent);
                mNotificationManager.notify(1,mNotification);
            }
        });
    }
}

SecondActivity.java :

package com.example.ch7;

import com.example.baseexample.R;

import android.app.Activity;
import android.os.Bundle;

public class SecondActivity extends Activity {

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ch7_second);
    }
}

AndroidManifest.xml 中application节点中添加 :

        <activity android:name="com.example.ch7.NotificationActivity" android:label="@string/app_name"></activity>
        <activity android:name="com.example.ch7.SecondActivity" android:label="@string/app_name"></activity>
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
android ContextMenu 上下文菜单示例
ch2\_contextmenu.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_p
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
东方客主 东方客主
3年前
Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条
问题1、页面布局文件:<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/layoutorderdetail"android:layoutwidth"matchparent"android:layoutheigh
Wesley13 Wesley13
2年前
android WebView 使用实例
主布局文件:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"
Stella981 Stella981
2年前
Android 复选框 以及回显
activity\_main.xml<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"http:/
Stella981 Stella981
2年前
Android控件ListView简易使用(使用ArrayAdapter)
<?xmlversion"1.0"encoding"utf8"?<TextViewxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/tv"android:la
Stella981 Stella981
2年前
Android选项卡TabHost功能和用法
1、布局文件<TabHostxmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.android.com/tools"android:id"@android:id/tabhost"
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这