实例
MainActivity.java
package com.example.notificationtest;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button sendBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBtn = (Button)findViewById(R.id.send_notice);
sendBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.send_notice:
sendNotice();
break;
default:
break;
}
}
public void sendNotice(){
Intent intent = new Intent(this,NoticeficationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);//第一个参数是context,第二
//个参数是一般直接传入0,第三个参数是Initent对象,第四个参数用来确定PendingIntent的行为(有FLAG_ONE_SHOT,
//FLAG_NO_CREATE,FLAG_CANCEL_CURRENT,FLAG_UPDATE_CURENT可以选择,具体用法自己百度)
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("这是一个通知")
.setContentText("这是通知的内容")
.setWhen(System.currentTimeMillis())
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pi)
.build();
manager.notify(1,notification);//第一个参数是ID要保证每个通知都有不同的ID,第二个参数是Notification对象。
}
}
PendingIntent和Intent的区别
PendingIntent倾向于在合适的时机执行,可以看做是延迟的Intent;Intent是立即执行,没有延迟。
NoticeficationActivity.java
package com.example.notificationtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class NoticeficationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.noticefication_layout);
}
}
布局文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.notificationtest.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/send_notice"
android:text="发送通知"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
noticefication_layout.xml
<?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:textSize="24sp"
android:text="这是通知时间布局"/>
</LinearLayout>