第一部分java文件:
package com.lenovo.dinghao1.mydialog.notification;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
import com.lenovo.dinghao1.mydialog.R;
public class MyNotification {
private Context mContext;
private NotificationManager notificationManager =null;
private Notification mNotification = null;
public MyNotification(Context context){
mContext = context;
init();
}
private void init(){
IntentFilter intent = new IntentFilter();
intent.addAction("android.intent.action_MTP");
intent.addAction("android.intent.action_PTP");
intent.addAction("android.intent.action_CHAR");
mContext.registerReceiver(new MyNotificationReceiver(),intent);
}
public void createCustomNotification(int code,String channel_id,String channel_name){
notificationManager = (NotificationManager) mContext.getApplicationContext().getSystemService(mContext.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel(channel_id,channel_name, NotificationManager.IMPORTANCE_HIGH);
channel.setLightColor(R.color.account_item_add);
channel.canShowBadge();
channel.enableVibration(false);
channel.getGroup();
channel.shouldShowLights();
notificationManager.createNotificationChannel(channel);
}
Notification.Builder builder = getBuilder(channel_id,"title","this is noti!");
builder.setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE);
RemoteViews remoteViews = null;
remoteViews = new RemoteViews(mContext.getApplicationContext().getPackageName(),R.layout.notifaction_layout);
if(remoteViews==null){
return;
}
builder.setCustomContentView(remoteViews);
Intent intentMtp = new Intent();
intentMtp.setAction("android.intent.action_MTP");
PendingIntent pendingIntentMtp = PendingIntent.getBroadcast(mContext.getApplicationContext(),-1,intentMtp,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btMpt,pendingIntentMtp);
Intent intentPtp = new Intent();
intentPtp.setAction("android.intent.action_PTP");
PendingIntent pendingIntentPtp = PendingIntent.getBroadcast(mContext.getApplicationContext(),-1,intentPtp,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btPtp,pendingIntentPtp);
Intent intentCharg = new Intent();
intentCharg.setAction("android.intent.action_CHAR");
PendingIntent pendingIntentCharg = PendingIntent.getBroadcast(mContext.getApplicationContext(),-1,intentCharg,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btCharging,pendingIntentCharg);
mNotification = builder.build();
notificationManager.notify(1000,mNotification);
}
public Notification.Builder getBuilder(String channel_id,String title,String context){
return new Notification.Builder(mContext.getApplicationContext())
.setAutoCancel(false)
.setChannelId(channel_id)
.setContentTitle(title)
.setContentText(context)
.setAutoCancel(true)
.setWhen(0)
.setOngoing(true)
.setTicker("ticker")
.setDefaults(0)
.setSmallIcon(R.mipmap.ic_launcher);
}
private class MyNotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent!=null){
if("android.intent.action_MTP".equals(intent.getAction())){
mNotification.contentView.setTextColor(R.id.btMpt,mContext.getResources().getColor(R.color.colorPrimary));
mNotification.contentView.setTextColor(R.id.btPtp,mContext.getResources().getColor(R.color.default_bt));
mNotification.contentView.setTextColor(R.id.btCharging,mContext.getResources().getColor(R.color.default_bt));
notificationManager.notify(1000,mNotification);
}else if("android.intent.action_PTP".equals(intent.getAction())){
mNotification.contentView.setTextColor(R.id.btMpt,mContext.getResources().getColor(R.color.default_bt));
mNotification.contentView.setTextColor(R.id.btPtp,mContext.getResources().getColor(R.color.colorPrimary));
mNotification.contentView.setTextColor(R.id.btCharging,mContext.getResources().getColor(R.color.default_bt));
notificationManager.notify(1000,mNotification);
}else if("android.intent.action_CHAR".equals(intent.getAction())){
mNotification.contentView.setTextColor(R.id.btMpt,mContext.getResources().getColor(R.color.default_bt));
mNotification.contentView.setTextColor(R.id.btPtp,mContext.getResources().getColor(R.color.default_bt));
mNotification.contentView.setTextColor(R.id.btCharging,mContext.getResources().getColor(R.color.colorPrimary));
notificationManager.notify(1000,mNotification);
}
}
}
}
}
第二部分布局xml文件notifaction_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:id="@+id/rlNotiBg"
android:layout_width="match_parent"
android:layout_height="120dp"
>
<TextView
android:id="@+id/btMpt"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="传文件"
/>
<TextView
android:id="@+id/btPtp"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true"
android:text="看照片"
android:textColor="@color/colorPrimary"
/>
<TextView
android:id="@+id/btCharging"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="仅充电"
android:textColor="@color/colorPrimary"
/>
</RelativeLayout>
</LinearLayout>
第三部分用法:
MyNotification myNotification = new MyNotification(mContext); myNotification.createCustomNotification(1,"10000","adbcdefg"); 注:已适配8.0以上通知不显示问题。
本文介绍了一种自定义Android通知栏的方法,通过创建NotificationChannel,使用RemoteViews设置自定义布局,并通过PendingIntent响应不同按钮操作。文章详细展示了如何在Android Oreo及以上版本中解决通知不显示的问题。
877

被折叠的 条评论
为什么被折叠?



