我知道如何显示通知,但是当我将它放在“onOptionsItemSelected”菜单上时,它不会创建通知。我很可能会遗漏一些东西,我只是不知道那是什么。继承人代码:
@TargetApi(android.os.Build.VERSION_CODES.KITKAT)
public void notification() {
etTitle = (EditText) findViewById(R.id.etTitle);
etNote = (EditText) findViewById(R.id.etNote);
String Title = etTitle.toString();
String Note = etNote.toString();
//boolean success = true;
notification = new NotificationCompat.Builder(this)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentTitle(Title)
.setContentText(Note)
.setDefaults(Notification.DEFAULT_ALL);
//vibrates when notification comes up
Notification note = notification.build();
note.defaults |= Notification.DEFAULT_VIBRATE;
note.flags |= Notification.FLAG_NO_CLEAR;
note.extras.getBoolean(Notification.EXTRA_SHOW_WHEN);
Intent resultIntent = new Intent();
resultIntent.putExtra(CommonConstants.EXTRA_MESSAGE, "hi");
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(),0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(resultPendingIntent);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, notification.build());
}
^这就是我创建通知的方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch(item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.action_save:
actionSave();
return true;
case R.id.action_delete:
actionDelete();
return true;
case R.id.Alarm:
return true;
case R.id.Pin:
notification();
return true;
}
return super.onOptionsItemSelected(item);
}
^那是我的onOptionsItemSelected菜单,R.id.Pin是创建通知的按钮。
任何建议都会有很大的帮助。谢谢。 注意:我只是java的入门级程序员,所以我可能不知道我想要学习的所有代码。