2017-06-06 36 views
0

(對不起我的英語)運行任務時,顯示通知

我需要用我的第一個程序,我必須作出週五通過對研究過程中幫助...

我想創建一個應用程序,該應用程序將顯示帶添加提醒的表格中的描述的通知。我在獲取下一個描述的機制方面遇到問題。顯示通知後,我對應用程序進行了描述。不幸的是,這在應用程序關閉時不起作用。

public void onReceive(Context context, Intent intent) { 
    ScheduleNotification.completeWakefulIntent(intent); 

    alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

    descToShow = d1; 

    DataBaseHandler dbh = new DataBaseHandler(context); 
    dbh.open(); 
    dbh.DeleteRow(descToShow); 
    dbh.close(); 


    Toast.makeText(context, descToShow, Toast.LENGTH_LONG).show(); 

    //notification 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
    builder.setSmallIcon(R.drawable.icon); 
    builder.setContentTitle(descToShow); 
    builder.setContentText(descToShow); //--description event-- 
    builder.setAutoCancel(false).build(); 

    //from android dev: 
    Intent resultIntent = new Intent(context, MainActivity.class); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 

    stackBuilder.addParentStack(MainActivity.class); 

    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.setContentIntent(resultPendingIntent); 
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    mNotificationManager.notify(1, builder.build()); 
    PendingIntent notifyPIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); 
    builder.setContentIntent(notifyPIntent); 
} 

任何想法?

我github上的所有代碼:https://github.com/azernax/dForget

+0

不要爲每個值使用單獨的'SharedPreferences' - 可以容納所有值。相關值應存儲在一個文件中 – Markaos

回答

0

的問題是,你用從你MainActivity靜態字段時MainActivity不在內存中(所以你得到的默認值,而不是那些onCreate()設置)。正確的解決方案是再次加載數據(在你的情況下從SharedPreferences)。

我希望這可以幫助你。如果您有任何問題,請留言

+0

感謝您使用SharedPreferences幫助解決我的問題。但我有新的問題:(例如,當我創建2通知,首先noti不顯示,只是第二次通知顯示在無限循環...你能幫助我嗎?當前代碼是在我的github上 – azernax

+0

你需要設置一個'NotificationBuilder'中的通知ID - 在你的情況下,對於d1,d2和d3擁有ID 1,2和3將是一個合理的想法 – Markaos