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;
ArrayList<String> listItems=new ArrayList<String>();
List<DataModel> DataModelList;
ListView listView;
public static final String Colloge_section="MyPrefs";
public NotificationFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
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);
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