android Notification 入门


package com.example.xh.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private NotificationManager mNotificationManager;
    private static final int NOTIFICATION_FLAG=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNotificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    }

    public void notificationMethod(View view) {
        Intent intent = new Intent(this,DemoActivity.class);//意图
        PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);//将intent传给pi
        Notification notification = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("有种你再点!")
                .setContentText("就知道你不敢点!")
                .setTicker("新消息")
                .setContentIntent(pi) //设置触摸通知后的行为
                .getNotification();
        notification.flags|=Notification.FLAG_AUTO_CANCEL;
       /* 旧版本支持
        Notification notification = new Notification();
        notification.icon=R.mipmap.ic_launcher;
        notification.tickerText="有新消息";
        notification.flags|=Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(this,"标题"."内容",pi);*/
        mNotificationManager.notify(NOTIFICATION_FLAG,notification);//调用notification
    }
}

DemoActivity.java

 

package com.example.xh.notification;

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

/**
 * Created by XH on 2016/8/18.
 */
public class DemoActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<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.xh.notification.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="千万不要点!"
        android:id="@+id/button"
        android:layout_marginTop="107dp"
        android:onClick="notificationMethod"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="能进到到这里说明你是一个充满好奇心的逗比!"
        android:textSize="50dp"
        android:textColor="@color/colorAccent"
        android:id="@+id/textView2"
        android:layout_marginTop="150dp"/>
</LinearLayout>

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值