Click here to Skip to main content
15,889,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created an app to get data from database on website and view it on listview , I want when the listview receive some new data to create notification, so for that I create a method to refresh listview every x second but it not working , it just creates notifications repeatedly . to know if the listview gets a new item I compare the old size of listview with new one, if it's the same I do nothing otherwise I create the notification. My code :
public class NotificationFragment extends Fragment {
Button loginButton;
//List<DataModel> listItems;
ArrayList<String> listItems=new ArrayList<String>();
List<DataModel> DataModelList;
ListView listView;
public static final String Colloge_section="MyPrefs";
public NotificationFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_notification, container, false);
    DataModelList = new ArrayList<>();
    listView =(ListView)view.findViewById(R.id.listview1);
    TextView name = (TextView)view.findViewById(R.id.name);
    buadd();
    new Timer().scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            buadd();

        }
    }, 0, 7000);//put here time 1000 milliseconds=1 second
    final SwipeRefreshLayout pullToRefresh = (SwipeRefreshLayout)view.findViewById(R.id.swipe_container);
    pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            DataModelList.clear();
            buadd();
            pullToRefresh.setRefreshing(false);
        }
    });
    return view;
}
public void buadd(){

    Thread runt=new Thread(){
        public void run(){
            CallNotservice cweb=new CallNotservice();
            cweb.OpenNotifications();}
    };
    runt.start();
    try{
        runt.join();
        String [] lst1=(ContactResult.ArrayMessage).split("&");
        MyListAdapter adapter = new MyListAdapter(getActivity(), R.layout.row, DataModelList);
        for (int i=lst1.length-1 ;i>=1;i--) {
            DataModelList.add(new DataModel(lst1[i]));
        }
        listView.setAdapter(adapter);

    }
    catch(Exception ex){
    }
    notifyThis("تطبيق جامعة كرري", "هنالك اشعار جديد , اضغط هنا لفتح التطبيق");

}
public void notifyThis(String title, String message) {
    SharedPreferences prfs = getActivity().getSharedPreferences("MyPrefs", MODE_PRIVATE);
    int Astatus = prfs.getInt("new_notify", 0);

    try {
        if (listView.getAdapter().getCount() != Astatus) {
            SharedPreferences.Editor editor = getActivity().getSharedPreferences(Colloge_section, MODE_PRIVATE).edit();
            editor.putInt("new_notify", listView.getAdapter().getCount());
            editor.apply();

        }
    }
    catch (Exception e){
        Toast.makeText(getContext(),"لاتوجد إشعارات جديده",Toast.LENGTH_LONG).show();
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getContext());
    mBuilder.setAutoCancel(true)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setTicker("{هنالك اشعار جديد في لوحة الاشعارات.}")
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.drawable.icon)
            .setContentInfo("تنبيه !");
    Intent notificationIntent = new Intent(getContext(), KworldActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(getContext(), 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(contentIntent);
    NotificationManager mNotificationManager =

            (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);

            mNotificationManager.notify(001, mBuilder.build());
}
}


What I have tried:

use doInBackground instead of timer
Posted
Comments
David Crow 28-Sep-18 12:14pm    
Your approach is not the most optimal or efficient. I suggest using a CursorLoader. See the discussion here for how the notification works so that you don't have to waste CPU cycles polling the data store.

While I've got a CP article that demonstrates this, plus a ton of other things that you'd have to weed through, there are tons of others available that are specific to CursorLoader and how it notifies "automatically."
Member 13366692 28-Sep-18 12:28pm    
can you help me with that !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900