0

我想建立一個肯定的應用程序,顯示通知作爲肯定的文本,當用戶點擊通知時,它應該把它帶到一個活動,到目前爲止,我能夠按小時顯示通知,但我不知道如何每小時顯示不同的通知,每次打開一個不同的活動,因爲會有隨機的確認列表,這些確認將顯示爲通知。每次我打開不同的活動時,如何顯示不同的通知?

編輯: 我的MainActivity:

private void hourly() 
     { 
     Calendar calender=Calendar.getInstance(); 
     Intent intent=new Intent(getApplicationContext(),notification_receiver.class); 
     PendingIntent pendingintent=PendingIntent.getBroadcast(this,100,intent,PendingIntent.FLAG_UPDATE_CURRENT); 
//  calender.setTimeInMillis(System.currentTimeMillis()); 
////  calender.set(Calendar.HOUR_OF_DAY, 10); 
////  calender.set(Calendar.MINUTE, 30); 
     alarmmanager=(AlarmManager)getSystemService(ALARM_SERVICE); 
     int interval=60000; 
     alarmmanager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
       interval, 
       interval, pendingintent); 
     Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show(); 
    } 

我notification_receiver類:

 public class notification_receiver extends BroadcastReceiver{ 
@Override 
public void onReceive(Context context, Intent intent) { 
    ArrayList<String> notificationTexts = null; 
    notificationTexts.add("This is a nice day"); // this will go to notificationTexts in position 0; 
    notificationTexts.add("This is nice morning"); 
    Random rand = new Random(); 
    int n = rand.nextInt(notificationTexts.size()); 
    NotificationManager notificationManager= (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent repeating_activity=new Intent(context,repeating_activity.class); 
    repeating_activity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent=PendingIntent.getActivity(context,100,repeating_activity,PendingIntent.FLAG_UPDATE_CURRENT); 
    NotificationCompat.Builder builder=new NotificationCompat.Builder(context) 
      .setContentIntent(pendingIntent) 
      .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark) 
      .setContentTitle("Affirmation") 
      .setContentText(notificationTexts.get(n)) 
      .setAutoCancel(true); 
      notificationManager.notify(100,builder.build()); 

} 

}

+1

什麼是肯定的應用程序?你也可以展示你做了什麼? – Juan

+0

如果它每小時選擇一個通知,每小時都會在手機上顯示一個通知,並且它會顯示一些正面的文本作爲標題,當用戶點擊文本時,它會將它帶到一個小時或每天的確認中在我的情況下,一些活動將成爲一個形象,寫在它上面的積極的想法,通知標題中顯示。 –

+0

你也可以檢查這個應用程序稱爲我的肯定它是很多類似於我想要做的。 –

回答

0

你已經有一個機制,以顯示你需要等什麼通知是一種的後端,您可以選擇一個隨機文本鏈接到您的應用程序的某個活動。

在您的主要活動,你可以有2個清單:

private ArrayList<Srtring> notificationTexts; 
private ArrayList<Class> activitiesToStart; 

蒙山這兩個添加文本您的肯定,並且希望在列表中的位置相同指數下啓動的活動類。

例如:

notificationTexts.add("This is a nice day"); // this will go to notificationTexts in position 0; 
activitiesToStart.add(TheNiceDayActivity.class); //this will go to the activitiesToStart also in position 0; 

在你已經有工作的機制,當你準備的通知,你只需要計算0之間,要麼名單的長度的隨機數,並選擇從文本第一個列表和第二個列表中的活動。要選擇相應的元素,請執行以下操作:list.get(position);

一旦獲得了該活動,就可以創建在構建通知時設置的PendingIntent。

我還沒有嘗試過,但它應該給你一個關於如何完成的想法。

編輯我

0和19(包括19)之間的隨機數可以用下式計算:

Random rand = new Random(); 
int n = rand.nextInt(20); 
+0

謝謝你解釋所有這些,但我不明白這裏的一些事情:1)我如何生成一個隨機數字?2)我如何根據隨機選擇的文本和活動更新我的通知標題和未決意圖? –

+0

你是如何構建你正在工作的通知的?編輯並將其添加到您的問題。 – Juan

+0

好吧,我會嘗試實施你在這裏解釋我的話,然後我會在這裏發佈完整的代碼,請訂閱帖子,並感謝先生。 –

相關問題