2011-09-06 71 views
3

我有一個創建通知的活動。當我使用和AVD模擬器(針對Android 2.1更新1)時,通知將啓動PendingIntent,但在實際設備(運行Android 2.2.1)上完全不啓動。有什麼我失蹤的根本?待定意圖不啓動

回答

1

您還沒有發表任何內容。因此,我分享我的代碼爲我工作:

public static void notifyIcon(Context context){ 

NotificationManager notifier = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
Notification notification = new Notification(R.drawable.your_app_notification, "", System.currentTimeMillis()); 

/** 
*Setting these flags will stop clearing your icon 
*from the status bar if the user does clear all 
*notifications. 
*/ 
    notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; 
    RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout); 
    notification.contentView = contentView; 

//If you want to open any activity on the click of the icon. 

    Intent notificationIntent = new Intent(context, YourActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
    notification.contentIntent = contentIntent; 

    notification.setLatestEventInfo(context, "title", null, contentIntent); 
    notifier.notify(1, notification); 

//To cancel the icon ... 
    notifier.cancel(1); 

} 

下面是custom_notification_layout.xml

<?xml version = "1.0" encoding = "utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="3dp" > 

</LinearLayout> 

注:請不要忘記在適當的時候如果上述標誌設置爲清除您的應用程序圖標。

+1

如果您在代碼中保留'notifier.cancel()',通知會顯示出來嗎?我的代碼幾乎完全相同,除了我的PendingIntent具有「FLAG_UPDATE_CURRENT」集並且我的通知具有「FLAG_AUTO_CANCEL」集。我不取消NotificationManager – vosmith

+0

notifier.cancel(1);是用來刪除通知,如果你不想刪除,然後離開它... –