我正在关注谷歌Android开发Android练习的基础教程。当涉及到通知时,我没有设置显示在通知左侧的图标。 教程是here。
public class MainActivity extends AppCompatActivity {
private ToggleButton mToggleButton;
private NotificationManager notificationManager;
private static final int NOTIFICATION_ID=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mToggleButton= findViewById(R.id.alarmToggle);
//改变togglebutton文字
//mToggleButton.setTextOff("Off");
// mToggleButton.setTextOn("On");
// mToggleButton.setText("Off");
mToggleButton.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
String toastMessage;
if (b){
toastMessage=getString(R.string.toggleButton_on);
deliverNotification(MainActivity.this);
}
else {
toastMessage=getString(R.string.toggleButton_off);
notificationManager.cancelAll();
}
Toast.makeText(MainActivity.this,toastMessage,Toast.LENGTH_SHORT).show();
}
});
}
public void deliverNotification(Context context){
Intent notificationIntent = new Intent(context,MainActivity.class);
PendingIntent notificationPendingIntent =PendingIntent.getActivity(
context,NOTIFICATION_ID,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
// NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"StandUp")
.setSmallIcon(R.drawable.ic_run)
//这里设定的图标是AS里随便找的一个,设置了没效果
//this doesn't work.ic_run comes from AS notifications image asset.
.setContentTitle(getString(R.string.notification_title))
.setContentText(getString(R.string.notification_text))
.setContentIntent(notificationPendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true);
// notificationManager.notify(NOTIFICATION_ID,builder.build());
notificationManager.notify(NOTIFICATION_ID,builder.build());
}
}
结果如下:通知的左侧是默认的android启动器
更新: 这次我遵循lesson。我将通知设置更改为:
mNotifyManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Battery Alert!")
.setContentText("Your battery is out of use.")
.setContentIntent(notificationPendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.addAction(R.drawable.ic_stat_name,"Learn More",learnMorePendingIntent)
.addAction(R.drawable.ic_update,"Update",updatePendingIntent)
.setAutoCancel(true)
.setDeleteIntent(dismissPendingIntent);//设置划掉通知产生的广播
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
notifyBuilder.setSmallIcon(R.drawable.ic_battery);
notifyBuilder.setColor(getResources().getColor(R.color.colorAccent));
}
else{
notifyBuilder.setSmallIcon(R.drawable.ic_battery);
}
答案 0 :(得分:0)
您应该根据API版本设置通知图标。
Notification notification = new NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notification.setSmallIcon(R.drawable.icon_transperent);
notification.setColor(getResources().getColor(R.color.notification_color));
} else {
notification.setSmallIcon(R.drawable.icon);
}