How To Send Sms In A Non-activity Class?
i have a service class, and a util class. in util class,i send a sms to a phone. but in util class ,there are many functions use params such as context,but the util extends nothing
Solution 1:
Just like this. set all pendingIntent null.
publicstaticbooleansmsSender(String to, String msg) {
SmsManager smsManager = SmsManager.getDefault();
try {
List<String> contents = smsManager.divideMessage(msg);
for (String content : contents){
smsManager.sendTextMessage(to, null, content, null, null);
}
returntrue;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
returnfalse;
}
}
Solution 2:
Try like this:
publicclassABCDextendsService{
Utilutil=newUtil(this);
}
publicclassUtil{
privateContextcontext=null;
publicUtil(Context context){
this.context = context;
}
}
Solution 3:
If I understand you right, you will have to pass the context as an argument when creating whatever object you are making with the util class.
So, I assume in your service class, you are making a Util class which sends the sms... I'll just make up some code that may fit what you are doing...
Within your service class, do this.
Utilutil=newUtil(this)
Doing that will create a Util object which has the context of the service, just make sure the Util class has a proper constructor to retain the context (for later use). Do this using this in your Util class
publicclassUtil {
private Context c //storage for the ContextpublicUtil(Context context){ //constructor for Utilthis.c = context;
}
}
Hope that helps.
Post a Comment for "How To Send Sms In A Non-activity Class?"