/backgroundService.java Secret
Created
November 4, 2012 13:24
Star
You must be signed in to star a gist
backgroundService.java
This file contains 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 romel.pi.redphone; | |
import java.util.Calendar; | |
import android.app.AlarmManager; | |
import android.app.PendingIntent; | |
import android.app.Service; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.os.IBinder; | |
import android.telephony.TelephonyManager; | |
import android.util.Log; | |
import android.widget.Toast; | |
public class BackgroundServices extends Service | |
{ | |
String tag="background service"; | |
private AlarmManager alarmManager; | |
private BroadcastReceiver alarmReceiver; | |
protected static String secondaryRedphone = "+6391xxxxx51"; | |
protected static String primaryRedphone = "+639xxxx03"; | |
protected PendingIntent pendingIntent; | |
private int minutes = 9; | |
String emailStatus = "xxx@stratloc.com"; | |
private static final String ALARM_REFRESH_ACTION = "romel.pi.redphone.ALARM_REFRESH_ACTION"; | |
SmsToEmail sendMail = new SmsToEmail(); | |
@Override | |
public void onCreate() | |
{ | |
super.onCreate(); | |
Log.i(tag, "Service created..."); | |
} | |
@Override | |
public void onStart(Intent intent, int startId) | |
{ | |
super.onStart(intent, startId); | |
Log.i(tag, "Service started..."); | |
startTimedService(); | |
} | |
@Override | |
public void onDestroy() | |
{ | |
super.onDestroy(); | |
Log.i(tag, "Service destroyed..."); | |
} | |
@Override | |
public IBinder onBind(Intent intent) | |
{return null;} | |
void startTimedService() | |
{ | |
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); // We get the AlarmManager | |
Intent intent = new Intent(ALARM_REFRESH_ACTION); // We prepare the pendingIntent for the AlarmManager | |
pendingIntent = PendingIntent.getBroadcast(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT); | |
cronService(); | |
IntentFilter filter = new IntentFilter(ALARM_REFRESH_ACTION); // We register dataReceiver to listen ALARM_REFRESH_ACTION | |
registerReceiver(alarmReceiver, filter); | |
startjob(); | |
} | |
void cronService() | |
{ | |
alarmReceiver = new BroadcastReceiver() | |
{ | |
@Override | |
public void onReceive(Context context, Intent intent) | |
{ | |
if(getMyPhoneNumber().contains(secondaryRedphone)) //Do not send SMS itself :D &function as checker if this app is installed in secondary redphone | |
{ | |
Log.i(tag, "Reading inbox"); | |
sendMail.sendToMail("OK", "secondary",emailStatus);//send out mail for monitoring | |
readInbox(); | |
} | |
else | |
{ | |
NotificationForwarderService sendSms = new NotificationForwarderService(); | |
sendSms.sendMultipart("OK",secondaryRedphone, context, intent); //send out SMS to secondary for monitoring | |
sendMail.sendToMail("OK", "primary",emailStatus);//send out mail for monitoring | |
} | |
} | |
}; | |
} | |
public void startjob() | |
{ | |
long intervals = minutes * 60000; | |
Calendar calendar = Calendar.getInstance(); // We have to register to AlarmManager | |
calendar.setTimeInMillis(System.currentTimeMillis()); | |
calendar.add(Calendar.SECOND, 1); | |
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), intervals, pendingIntent); // We set a repeating alarm | |
Toast.makeText(BackgroundServices.this,"Job Started", Toast.LENGTH_SHORT).show(); | |
} | |
public void stopJob() | |
{ | |
// We cancel alarms that matches with pending intent | |
alarmManager.cancel(pendingIntent); | |
Toast.makeText(this, "Job stopped", Toast.LENGTH_SHORT).show(); | |
} | |
public String getMyPhoneNumber() | |
{ | |
TelephonyManager mTelephonyMgr; | |
mTelephonyMgr = (TelephonyManager) | |
getSystemService(Context.TELEPHONY_SERVICE); | |
return "+63"+mTelephonyMgr.getLine1Number(); | |
} | |
public void readInbox() | |
{ | |
Uri uriSMSURI = Uri.parse("content://sms/inbox"); | |
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null); | |
String status = "null"; | |
while (cur.moveToNext()) | |
{ | |
if(cur.getString(2).contains(primaryRedphone) && cur.getString(11).contains("OK"))//Search msg from Primary redphone with 'OK' body | |
{ | |
status = cur.getString(11); //Update status | |
long thread_id = cur.getLong(1); //Get the ID of 'OK' message | |
deleteSms(thread_id); //delete the 'OK' message | |
} | |
} | |
if(status.equals("null")) | |
sendMail.sendToMail(status, status, emailStatus); | |
else | |
sendMail.sendToMail("OK","OK", emailStatus); | |
Log.i(tag, "email sent"); | |
} | |
public void deleteSms(long thread_id) | |
{ | |
Uri deleteUri = Uri.parse("content://sms/inbox"); | |
Cursor c = getContentResolver().query(deleteUri, null, null, null, null); | |
while(c.moveToNext()) | |
{ | |
Uri thread = Uri.parse("content://sms/conversations/" + thread_id); | |
getContentResolver().delete(thread, null, null); | |
Log.i(tag, "SMS deleted"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment