Notification:
例子的NotificationCompat使用的是v4包下的,稍微有点老,如果大家使用的话,建议使用v7包下的,通知也没什么好介绍的,直接看代码
MainActivity:
package com.fitsoft;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private int notificationId = 100;
private static int times = 0;
Button button;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this; //设置上下文对象
button = findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
times++;
if(times%2 == 1){
button.setText("销毁通知");
createNotification(); //创建通知
}else {
button.setText("创建通知");
destroyNotification(); //取消通知
}
}
});
}
/**
* 创建通知
*/
private void createNotification() {
//创建builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "my_notifications");
//设置属性
builder.setSmallIcon(R.mipmap.ic_launcher_round); //设置图标
builder.setContentTitle("这是通知的标题"); //设置标题
builder.setContentText("这是通知的具体内容"); //设置内容
builder.setNumber(1); //设置通知次数
builder.setAutoCancel(true); //设置是否自动取消
//构建启动意图
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1,
new Intent(this, SecondActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
//设置启动意图
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
//获取系统服务
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//系统创建通知
notificationManager.notify(notificationId, notification);
}
/**
* 取消通知
*/
private void destroyNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//取消通知
notificationManager.cancel(notificationId);
}
}
布局文件使用的是上一次的只有一个按钮的状态选择器的布局,具体请查看上一篇。
这里的方法都很好理解,只是其中有一个构建启动意图的时候的PendingIntent.getActivity()方法,我们具体看一下:
/**
* Retrieve a PendingIntent that will start a new activity, like calling
* {@link Context#startActivity(Intent) Context.startActivity(Intent)}.
* Note that the activity will be started outside of the context of an
* existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK
* Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent.
*
* <p class="note">For security reasons, the {@link android.content.Intent}
* you supply here should almost always be an <em>explicit intent</em>,
* that is specify an explicit component to be delivered to through
* {@link Intent#setClass(android.content.Context, Class) Intent.setClass}</p>
*
* @param context The Context in which this PendingIntent should start
* the activity.
* @param requestCode Private request code for the sender
* @param intent Intent of the activity to be launched.
* @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE},
* {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT},
* or any of the flags as supported by
* {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
* of the intent that can be supplied when the actual send happens.
*
* @return Returns an existing or new PendingIntent matching the given
* parameters. May return null only if {@link #FLAG_NO_CREATE} has been
* supplied.
*/
public static PendingIntent getActivity(Context context, int requestCode,
Intent intent, @Flags int flags) {
return getActivity(context, requestCode, intent, flags, null);
}
这里的第四个参数flags值得注意,我们点击这个@Flags注解进行查看:
/** @hide */
@IntDef(flag = true,
value = {
FLAG_ONE_SHOT,
FLAG_NO_CREATE,
FLAG_CANCEL_CURRENT,
FLAG_UPDATE_CURRENT,
FLAG_IMMUTABLE,
Intent.FILL_IN_ACTION,
Intent.FILL_IN_DATA,
Intent.FILL_IN_CATEGORIES,
Intent.FILL_IN_COMPONENT,
Intent.FILL_IN_PACKAGE,
Intent.FILL_IN_SOURCE_BOUNDS,
Intent.FILL_IN_SELECTOR,
Intent.FILL_IN_CLIP_DATA
})
@Retention(RetentionPolicy.SOURCE)
public @interface Flags {}
这是一个源码级的注解,有五个常用的值以及一些Intent的常用值,这五个值的含义大致如下:
- FLAG_ONE_SHOT:表明这里构建的PendingIntent只能使用一次
- FLAG_NO_CREATE:如果前一个PendingIntent已经不存在,将不再构建它
- FLAG_CANCEL_CURRENT:如果构建的PendingIntent已经存在,则取消前一个,重新构建一个
- FLAG_UPDATE_CURRENT:如果构建的PendingIntent已经存在,则替换它
- FLAG_IMMUTABLE:已经构建的PendingIntent不可更改
这里我们使用第4个,因为它比较常用。
效果图: