目录
Notification
- Notification的常用方法
setContentTitle(String string):设置标题
setContentText(String string):设置文本内容 setSmallIcon(int icon):设置小图标
setLargeIcon(Bitmap icon):设置通知的大图标 setColor(int argb):设置小图标的颜色
setContentIntent(PendingIntent intent):设置点击通知后的跳转意图
setAutoCancel(boolean boolean):设置点击通知后自动清除通知
setWhen(long when):设置通知被创建的时间
- 通知渠道:Android8.0引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。通知重要程度设置,NotificationManager类中:
- IMPORTANCE_NONE:关闭通知
- IMPORTANCE_MIN:开启通知,不会弹出,没有提示音,状态栏中无显示
- IMPORTANCE_LOW:开启通知,不会弹出,不发出提示音,状态栏中显示
- IMPORTANCE_DEFAULT:开启通知,不会弹出,发出提示音,状态栏中显示
- IMPORTANCE_HIGH:开启通知,会弹出,发出提示音,状态栏中显示
完整代码如下(实现发送通知和取消通知):
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/sendnotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendNotification"
android:text="发送通知"
android:textSize="24sp"
android:layout_gravity="center"
android:layout_marginTop="200dp"/>
<Button
android:id="@+id/cacelnotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="cancelNotification"
android:text="取消通知"
android:textSize="24sp"
android:layout_gravity="center"
android:layout_marginTop="20dp"/>
</LinearLayout>
MainActivity:
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
public class MainActivity extends AppCompatActivity {
private NotificationManager manager;
private Notification notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//版本判断(因为通知渠道是Android8.0才引入的)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("eat","测试通知",NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}
Intent intent = new Intent(this,NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
//channelld是通知渠道的id
notification = new NotificationCompat.Builder(this,"eat")
.setContentTitle("吃饭通知")
.setContentText("该吃饭了!快过来!")
.setSmallIcon(R.drawable.ic_baseline_accessibility_24)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.tupian))
.setColor(Color.parseColor("#ff0000"))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
}
public void sendNotification(View view) {
manager.notify(1,notification);
}
public void cancelNotification(View view) {
manager.cancel(1);
}
}
NotificationActivity:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
public class NotificationActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("eat","成功进入");
}
}
总结一下实现的过程:
首先先通过getSystemService创建NotificationManager,之后判断版本是否在8.0以上,若是便可以创建NotificationChannel,之后再用createNotificationChannel方法,使用NotificationCompat.Builder创建Notification的对象(必须使用setContentTitle、setContentText、setSmallIcon这三个属性,其他自愿使用),最后发送通知使用notify,取消使用cancel。
运行结果截图: