2013-05-16 47 views
2

如何在Android的通知欄中顯示簡單通知?請幫助我使用最簡單的解決方案。如何在android中顯示簡單通知?

+1

你谷歌「機器人通知」?第一個結果似乎是adroid開發者「如何創建通知」的指南。這是我們可以期待的大量研究 – LionC

+0

是的,我做過。但他們都似乎有點混亂。我想要的只是一個簡單的方法。 – Andromise

+2

[Notification](http://developer.android.com/reference/android/app/Notification.html)或[Toasts](http://developer.android.com/guide/topics/ui/notifiers/toasts。 html)? – Bishan

回答

6

我假設你是問有關通知,notificationbar.If其如此,試試這個代碼,

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. 
    mNM.notify("Title", 0, notification); 
} 
+0

是mNM嗎?我的通知? –

+0

@WHK:它的通知管理器 –

8

這很容易。

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

} 

您可以將聲音或振動添加到通知,以及:在這個例子中單擊該按鈕時,通知被觸發。欲瞭解更多信息,你可以看到this教程。

+0

儘管上面提到的通知構造函數已被棄用......仍然是+1 ... Notification.Builder()是創建通知的新方法。 –

+0

謝謝@JohnnyWu – erdomester