应用程序未打开时,服务中的sharedpreferences不会保存

时间:2017-02-01 08:18:13

标签: android listview android-service android-sharedpreferences

当我发送通知并且应用程序尚未打开时,我无法在mainactivity的listview上看到我的通知。当我发送通知并且应用程序已经打开时,我在listview上看到。每次发送通知时如何在listview上看到通知?

我的服务

    private void sendNotification(String messageBody) {
    SharedPreferences a=getSharedPreferences("noti",MODE_PRIVATE);
    SharedPreferences.Editor editor=a.edit();
    editor.putInt("i",a.getInt("i",0)+1);
    editor.putString(String.valueOf(a.getInt("i",0)+1),messageBody);
    editor.apply();

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.farmer)
            .setContentTitle("Zengin Çiftçi")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

我的主要活动

   a=getSharedPreferences("noti",MODE_PRIVATE);
    for(int i=a.getInt("i",0);i>0;i--){
        liste.add(a.getString(String.valueOf(i),""));
    }

0 个答案:

没有答案