2012-11-09 104 views
2

我是一名Android初學者。我想在我的應用程序中顯示通知。我用下面的代碼在Android中顯示通知而無需使用意圖

 String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager notificationManager = (NotificationManager) getSystemService(ns); 
     int icon = R.drawable.icon; 
     CharSequence tickerText = ""; 
     long when = System.currentTimeMillis(); 
     Notification notification = new Notification(icon, tickerText, when); 
     Context context = getApplicationContext(); 
     CharSequence contentTitle = "Hello"; 
     CharSequence contentText = "Welcome "; 
     Intent notificationIntent = new Intent(); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, null, 0); 
     notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);  
     notificationManager.notify(HELLO_ID, notification); 

但我的問題是,每當運行我的應用程序的意圖加載圖標。然後將圖標顯示在狀態欄。如何在不使用意圖的情況下顯示通知?請幫助

回答

0

我不知道這是否是你想要的,但如果你想展示一些快速通知,吐司浮現在腦海:

Context context = getApplicationContext(); 
    CharSequence text = "Notification!"; 
    int duration = Toast.LENGTH_SHORT; 
    Toast toast = Toast.makeText(context, text, duration); 
    toast.show(); 

可以更改吐司多久顯示與持續時間。