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,just a simple class. how to do it ?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Just like this. set all pendingIntent null.
public static boolean smsSender(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);
}
return true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
}
回答2:
Try like this:
public class ABCD extends Service{
Util util = new Util(this);
}
public class Util{
private Context context = null;
public Util(Context context){
this.context = context;
}
}
回答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.
Util util = new Util(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
public class Util {
private Context c //storage for the Context
public Util (Context context){ //constructor for Util
this.c = context;
}
}
Hope that helps.