mardi 6 février 2018

How to make service class extends another class as well?

I'm going to read some data from server each time and then show them as notification to the user. So I have a class that extends Service and I also have another class that's called GetNotifications and ServiceClass uses it to do some functions.

this is my ServiceClass:

public class ServiceClass extends Service {

public Context context = this;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {

    shared= PreferenceManager.getDefaultSharedPreferences(context);
    handler = new Handler();
    runnable = new Runnable() {
        public void run() {       
            boolean isregistered=Utils.Checking_Running_Service(shared);
            if (isregistered)
            {
                GetNotifications getNoti=new GetNotifications(context);
                getNoti.getNotificationRequest();
            }
            handler.postDelayed(runnable, 5000);
        }
    };
    handler.postDelayed(runnable, 1000);
}
}

and this is my GetNotifications class:

 public class GetNotifications extends MyActivity{
 Context mycontext;

 public GetNotifications(Context context){
   mycontext=context;
}

public void getNotificationRequest() {

    if (Utils.isOnline(mycontext)) {

        HashMap<String, String> hashMap = new HashMap<>();
        .
        .
        .
        WebServiceManager webServiceManager = new WebServiceManager(???, 0, "get");
        webServiceManager.execute(hashMapForWeb);
    }
}
}...//and some another functions for getting result from webservice

For some reasons I need to use a MyActivity context as input of WebServiceManager class.(where I used ??? above). actually I need to make MyActivity myactivity=this; and use myactivity as input of WebServiceManager class.but I get error that you cant call "this" before onCreate. I have to say I dont want to startActivity from my service class to GetNotification Activity.so if I even have onCreate,it wont call. (instead of using startActivity in service class I just want to call getNotificationRequest method as I wrote above.because if I use startActivity,it will show me a white page each time I send notification). So I think if my service class could extend MyActivity as well,I would use its context for my aim:|

Aucun commentaire:

Enregistrer un commentaire