0

I have a class inside which there is another class that extends AsyncTask. As,

public class Dashboard extends Activity {
 -------------
 --------------
 --------------
  public class getUnreadMessageCount  extends AsyncTask<String, String, JSONObject> {
    protected JSONObject doInBackground(){
     *********some database stuff************
    }
    protected void onPostExecute(JSONObject json) {
       count = Integer.parseInt(json.getString("messsage_count"));
       // Set Text to the textview
       messageCount.setText(count); 
    }

  }
}

Now I am using GCMIntentService Class for notification. How can I call and execute the getUnreadMessageCount in the following

private void sendNotification(String content_id, int type, String msg) {

}

I tried as

private void sendNotification(String content_id, int type, String msg) {
    Dashboard dashboard = new Dashboard;
    dashboard.Dashboard dashboard---- 
}

But it does not work. How could I solve this problem.

Nepal12
  • 583
  • 1
  • 12
  • 29

1 Answers1

0

What I see here is very bad style and not according to the Java-Code-Conventions. You are naming a class like a method. In java generally, all Class names are written in capital letters and should be nouns.

public class getUnreadMessageCount  extends AsyncTask

should be called (at least):

public class GetUnreadMessageCount  extends AsyncTask

or much better:

// make clear it is an async task
public class GetUnreadMessageCountTask  extends AsyncTask

This is how to start the AsyncTask:

private void sendNotification(String content_id, int type, String msg) {
     new GetUnreadMessageCountTask().execute();
}

The execute(...) method can also take parameters, depending on your specification of the AsyncTask. For a general explaination of the AsyncTask, have a look here: How to use AsyncTask

Community
  • 1
  • 1
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • I did change the class name to GetUnreadMessageCount as you've suggested. But I can call it only from the class within . I cannot call It from any other class. – Nepal12 Mar 02 '14 at 22:52
  • If you want to call it from another class, make a separate file for the GetUnreadMessageCount class and then import it wher you need it. – Philipp Jahoda Mar 02 '14 at 22:56
  • Yes, but I need to extend view if I need to access the Layout but how could I do it if I extend a class with AsyncTask. If you see my Class with AsyncTask I have implemented `count = Integer.parseInt(json.getString("messsage_count")); // Set Text to the textview messageCount.setText(count); ` – Nepal12 Mar 02 '14 at 23:03