Issue
I have to send getApplicationContext to a method, and in this method, I have to cast context to activity to use
getWindowManager().getDefaultDisplay().getMetrics(dm)
But it show an exception
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity.
Why getApplicationContext?because I check my application update in main activity and don't want to limit it to an Activity (I want lifecycle).
How handle it?
UPDATE
I have FN.java that all static functions are here. one method job (getUniqueID) is to get device ID and screen width and height and etc. IN another hand, I have a Update.java that's for checking application update.
In this class, I use getUniqueID.
For using Update class in my main activity, I do it like :
if(updateInstance == null)
updateInstance = new Update(getApplicationContext());
If I pass this instead of getApplicationContext, I know it's ok, but I want use getApplicationContext because maybe user click on some menu item and go to another activity or close application immediately; And I want to show notification Anyway.
Solution
I just use
this
and everything works fine.
And for update check use singleton.
Answered By - Dr.jacky
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.