Created
July 22, 2013 15:26
-
-
Save skatkov/6054743 to your computer and use it in GitHub Desktop.
SmsService...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ee.era.geek.sms_spammer.service; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.os.*; | |
import android.telephony.SmsManager; | |
import android.text.format.DateFormat; | |
import android.util.Log; | |
import ee.era.geek.sms_spammer.ConfigUtil; | |
import java.util.Date; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import static java.util.concurrent.TimeUnit.*; | |
public class SmsService extends Service { | |
private Messenger messenger; | |
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | |
public static final String MESSENGER = "MESSENGER"; | |
@Override | |
public int onStartCommand(final Intent intent, final int flags, final int startId) { | |
if (messenger == null){ readIntent(intent);} | |
Log.i(getClass().toString(), "Read message data"); | |
final ConfigUtil conf = ConfigUtil.getInstance(getApplicationContext()); | |
final String number = conf.getNumber(); | |
final String msg = conf.getMsg(); | |
final Integer min = Integer.parseInt(conf.getMin()); | |
final Integer max = Integer.parseInt(conf.getMax()); | |
final Runnable smsTask = new Runnable() { | |
public void run() { | |
int val = getRandomInterval(min, max); | |
Log.i(getClass().toString(), "Sending message to " + number + | |
" next interval in " + val + " SECONDS"); | |
sendSms(number, msg); | |
sendMessageToUi("Sending message to " + number + | |
" next interval in " + val + " SECONDS"); | |
scheduler.schedule(this, val, SECONDS); | |
} | |
}; | |
scheduler.schedule(smsTask, getRandomInterval(min, max), SECONDS); | |
return super.onStartCommand(intent, flags, startId); | |
} | |
public static int getRandomInterval(int min, int max){ | |
return min + (int)(Math.random() * ((max - min)+1)); | |
} | |
private void readIntent(Intent intent) { | |
Log.i(getClass().toString(), "Reading intent.."); | |
Bundle extras = intent.getExtras(); | |
if (extras != null) { messenger = (Messenger) extras.get(MESSENGER);} | |
} | |
private void sendSms(String number, String message){ | |
SmsManager sm = SmsManager.getDefault(); | |
sm.sendTextMessage(number, null, message, null, null); | |
} | |
private void sendMessageToUi(String message) { | |
try { | |
messenger.send(makeMessage(message)); | |
} catch (RemoteException e) { | |
Log.w(getClass().getName(), "Exception sending message to UI", e); | |
} | |
} | |
private Message makeMessage(String message) { | |
Message msg = Message.obtain(); | |
msg.obj = DateFormat.format("yyyy-MM-dd hh:mm:ss ", new Date().getTime()).toString() + message; | |
return msg; | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
@Override | |
public void onDestroy() { | |
Log.i(getClass().getName(), "onDestroy fired..."); | |
scheduler.shutdownNow(); | |
super.onDestroy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment