我正在尝试运行以下内容,这是从互联网教程中删除的:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Prepare intent which is triggered if the
// notification is selected
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
//Intent intent = new Intent(this, ReceiveAndGoHome.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, homeIntent, 0);
// Build notification
// Actions are just fake
Notification noti = new NotificationCompat.Builder(this)
.setContentTitle("Fraz Go Home!!!")
.setContentTitle("Fraz Go Home!!!")
.setContentText("Fraz Go Home!!!")
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(25, noti);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
当我在froo AVD中运行上述内容时,我看不到任何通知 - 我是否遗漏了一些明显的内容?
答案 0 :(得分:3)
您忘了设置小图标。加上这个:
.setSmallIcon (R.drawable.ic_launcher)
在NotificationCompat.Builder
方法链中的某个地方。
当然,启动器drawable是我在那里使用的,因为它在手边,你需要为适当的UI制作实际的通知图标。这只是为了演示你需要的方法调用。
Android documentation概述了通知显示所需的内容:
必填通知内容
通知对象必须包含 以下内容:
•由setSmallIcon()
设置的小图标•标题,由setContentTitle()
设置•详细文本,由setContentText()
设置