android Notification 入门

来源:互联网 发布:淘宝外围女孙佑怡 编辑:程序博客网 时间:2024/06/01 22:11

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>




0 0
原创粉丝点击