Java 如何在android中显示简单的通知?

Java 如何在android中显示简单的通知?,java,android,Java,Android,如何在Android的通知栏中显示简单的通知?请帮助我找到最简单的解决方案。我猜您是在询问notificationbar中的通知。如果是这样,请尝试以下代码 private void showNotification(String eventtext, Context ctx) { // Set the icon, scrolling text and timestamp Notification notification = new Notification(R.draw

如何在Android的通知栏中显示简单的通知?请帮助我找到最简单的解决方案。

我猜您是在询问notificationbar中的通知。如果是这样,请尝试以下代码

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);
}

这很容易。在本例中,单击按钮时会触发通知:

<?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.
            }
        });
    }

}

公共类MainActivity扩展了活动{
按钮btn;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(按钮)findViewById(R.id.button1);
btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//我们得到了通知经理的推荐信
NotificationManager NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
字符串MyText=“提醒”;
Notification mNotification=新通知(R.drawable.Notification_图标,MyText,System.currentTimeMillis());
//这三个参数是:1.图标,2.标题,3.通知出现的时间
字符串MyNotificationTitle=“Medicine!”;
String MyNotificationText=“别忘了吃药!”;
Intent MyIntent=新意图(Intent.ACTION\u视图);
pendingent startinent=pendingent.getActivity(getApplicationContext(),0,MyIntent,pendingent.FLAG_CANCEL_CURRENT);
//单击通知时将触发挂起事件。当前标志取消挂起事件
mNotification.setLatestEventInfo(getApplicationContext(),MyNotificationTitle,MyNotificationText,StartIntent);
int通知_ID=1;
notificationManager.notify(通知ID、通知);
//我们正在使用唯一id将通知传递给NotificationManager。
}
});
}
}

您还可以在通知中添加声音或振动。有关更多信息,请参阅教程。

是的,我看过。但它们似乎都有点混乱。我想要的只是一个简单的方法。或者?@Andromise发布您阅读过的代码,并告诉我们您在哪里感到困惑,回答这个问题的人我不理解,如果没有他的任何研究努力,您如何直接提供代码。2018检查简单的最新方法@WHK:its Notification manager尽管提到了通知构造函数不推荐使用…仍然+1…Notification.Builder()是创建通知的新方法。