2012-06-24 241 views
0

我正在開發一個android應用程序,並且我正在嘗試創建通知,並且一旦用戶單擊該通知,應該啓動一個活動。通知創建正常,但活動未開始。從通知開始活動

以下是通知的代碼。

private void showNotification(String username, String password) 
{ 
    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(ns); 

    CharSequence tickerText = "Username Copied"; 
    long when = System.currentTimeMillis(); 

    int icon = R.drawable.icon; 

    Notification notification = new Notification(icon, tickerText, when); 
    CharSequence contentTitle = "BPM - Login Management"; 
    CharSequence contentText = "Username Copied"; 

    Intent intentNotification = new Intent(context, CopyPassword.class); 
    intentNotification.setAction(Long.toString(when)); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    intentNotification.putExtra("password", password); 

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotification, PendingIntent.FLAG_UPDATE_CURRENT); 
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

    final int NOTIFICATION_ID = 1; 
    notificationManager.notify(NOTIFICATION_ID, notification); 
} 

這個通知是從一個普通的java類中產生的,而不是一個android活動,如果這有什麼區別的話。

感謝您的幫助。

+0

你爲什麼這樣做? 'intentNotification.setAction(Long.toString(當));'? 「Intent」的動作應該像「ACTION_VIEW」(例如)。將動作設置爲代表當前系統時間的任意值是沒有意義的。 – Squonk

+0

我不知道我在那裏做什麼,我現在已經刪除了。我已更改爲使用Intent.ACTION_VIEW,但沒有什麼區別,只是完全刪除它並沒有什麼區別 – Boardy

+0

您正在顯示的代碼是否在廣播接收器類中? –

回答

2

刪除setAction,這是不需要的。

嘗試:

Intent intentNotification = new Intent(context, CopyPassword.class); 
intentNotification.putExtra("password", password); 

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotification, 0); 

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

還要確保CopyPassword活動在您AndroidManifest

宣佈我寫了一篇博客文章吧:http://blog.blundell-apps.com/notification-for-a-user-chosen-time/ :-D

+0

謝謝是因爲我忘了將活動添加到清單文件。我總是忘記這一點,但因爲它似乎沒有在通知中啓動活動的日誌貓中拋出異常。 – Boardy