我正在開發一個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活動,如果這有什麼區別的話。
感謝您的幫助。
你爲什麼這樣做? 'intentNotification.setAction(Long.toString(當));'? 「Intent」的動作應該像「ACTION_VIEW」(例如)。將動作設置爲代表當前系統時間的任意值是沒有意義的。 – Squonk
我不知道我在那裏做什麼,我現在已經刪除了。我已更改爲使用Intent.ACTION_VIEW,但沒有什麼區別,只是完全刪除它並沒有什麼區別 – Boardy
您正在顯示的代碼是否在廣播接收器類中? –