Java 如何在android中显示简单的通知?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16584438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I display a simple notification in android?
提问by Andromise
How can I display a simple notification in notification bar in Android? Please help me with the easiest solution.
如何在 Android 的通知栏中显示简单的通知?请帮我解决最简单的问题。
采纳答案by Basim Sherif
I assume you are asking about notification in notificationbar.If its so,Try this code,
我假设你在通知栏中询问通知。如果是这样,试试这个代码,
private void showNotification(String eventtext, Context ctx) {
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.noti_icon,
text, System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this
// notification
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
new Intent(ctx, MainActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(ctx, "Title", eventtext,
contentIntent);
// Send the notification.
mNotificationManager.notify("Title", 0, notification);
}
回答by erdomester
It's easy. In this example a notification is fired when the button is clicked:
这很简单。在此示例中,单击按钮时会触发通知:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#A9BCF5"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:text="Create Notification" >
<Button>
<LinearLayout>
public class MainActivity extends Activity {
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//We get a reference to the NotificationManager
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "Reminder";
Notification mNotification = new Notification(R.drawable.notification_icon, MyText, System.currentTimeMillis() );
//The three parameters are: 1. an icon, 2. a title, 3. time when the notification appears
String MyNotificationTitle = "Medicine!";
String MyNotificationText = "Don't forget to take your medicine!";
Intent MyIntent = new Intent(Intent.ACTION_VIEW);
PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
//A PendingIntent will be fired when the notification is clicked. The FLAG_CANCEL_CURRENT flag cancels the pendingintent
mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
int NOTIFICATION_ID = 1;
notificationManager.notify(NOTIFICATION_ID , mNotification);
//We are passing the notification to the NotificationManager with a unique id.
}
});
}
}
You can add a sound or vibration to the notification as well. For more information, you can see thistutorial.
您也可以向通知添加声音或振动。有关更多信息,您可以查看本教程。