2017-09-11 101 views
0
.setWhen(System.currentTimeMillis()) 

是在推送通知android中設置時間的代碼,結果就像15.54。但是如果我想在幾秒鐘的時間內像這樣設置時間15:54:02我應該怎麼做?你能幫我或提供示例源代碼嗎?如何在第二次設置通知的時間和日期?

這是我的通知管理器源代碼。

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx); 
    Notification notification; 
    notification = mBuilder.setSmallIcon(R.drawable.logo).setTicker(title).setWhen(0) 
      .setAutoCancel(true) 
      .setContentIntent(resultPendingIntent) 
      .setContentTitle(title) 
      .setSmallIcon(R.drawable.logo) 
      .setWhen(System.currentTimeMillis()) 
      .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher)) 
      .setContentText(message) 
      .build(); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.flags |= Notification.FLAG_SHOW_LIGHTS; 

    notification.defaults = Notification.DEFAULT_SOUND; 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notification.defaults = Notification.DEFAULT_ALL; 
    int id = (int) System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(ID_SMALL_NOTIFICATION, notification); 
    notificationManager.notify(id, notification); 
} 
+0

以'notification.defaults'擺弄看起來無意義的給我。我會投票刪除這個,不要混淆讀者,無論是在這裏還是在實際的代碼庫中。 –

回答

0

您可以設置或不符合毫秒.setWhen()在默認情況下獲得。

時間格式你想要顯示的像15:54:02 24小時制格式。沒有時間格式NotificationCompat.Builder類的設置。

什麼都設置毫秒轉換爲時間顯示在通知中,如15:54沒有秒。

你可以做的是使用自定義通知視圖而不是默認視圖。

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 
    String formattedDate = ""; 
    formattedDate = sdf.format(new Date()); 

    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification); 
    contentView.setImageViewResource(R.id.image, R.drawable.logo); 
    contentView.setTextViewText(R.id.title, title); 
    contentView.setTextViewText(R.id.text, message); 
    contentView.setTextViewText(R.id.time, formattedDate); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.home) 
      .setContent(contentView); 

    Notification notification = mBuilder.build(); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.defaults |= Notification.DEFAULT_SOUND; 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    NotificationManager notificationManager = (NotificationManager) MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE); 
    int id = (int) System.currentTimeMillis(); 
    notificationManager.notify(id, notification); 

佈局..

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:gravity="left|center_horizontal|center_vertical" 
      android:weightSum="4"> 

<RelativeLayout 
     android:id="@+id/layout" 
     android:layout_width="0dp" 
     android:layout_weight="3" 
     android:layout_height="64dp" 
     android:padding="10dp" > 
    <ImageView 
     android:src="@mipmap/ic_launcher" 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="10dp" /> 
    <TextView 
     android:textSize="13dp" 
     android:textColor="#000" 
     android:text="Testing" 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/image" 
     /> 
    <TextView 
     android:textSize="13dp" 
     android:textColor="#000" 
     android:text="Testing is awecome" 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/image" 
     android:layout_below="@id/title" 
     /> 
</RelativeLayout> 

<TextView 
    android:textSize="13dp" 
    android:textColor="#000" 
    android:text="Testing" 
    android:id="@+id/time" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:paddingRight="20dp" 
    android:gravity="right|center_vertical|center_horizontal" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/image"/> 

    </LinearLayout> 

輸出這樣的...

enter image description here

+0

謝謝,我會嘗試 –

+0

你最歡迎.. –

1

但是,如果我想成立時間像這樣15時54分02秒

你不能與內置的通知佈局。如果你真的需要秒,你需要有自定義通知佈局。但我強烈反對這一點。

+0

但我需要它(第二次)它沒有問題,如果放在我的消息不在我的通知。我只需要第二次在手機中收到我的通知時。因爲我的程序是發送網絡監控通知。我想分析從服務器發送的時間,然後在我的智能手機中接收。或者你有什麼建議如何計算延遲我的通知? –

0

對於這一點,你需要的是時間「14點54分02秒」轉換以毫秒爲單位,並設置結果時間.setWhen()

-1
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss"); 

String dateString = formatter.format(new Date(dateInMillis))); 

       

相關問題