This what i'm trying to do.
I have main Activity calling BroadcastReceiver name SmsAlarmReceiver.
Intent i = new Intent(SmsAlarmReceiver.ALARM_ACTION);
sendBroadcast(i);
Now my SmsAlarmReceiver.java looks like:
public class SmsAlarmReceiver extends BroadcastReceiver {
LocationManager locationmanager;
public static final String ALARM_ACTION= "com.example.finaltracking.SMS_REC";
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
locationmanager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Intent smsintent = new Intent(context,SmsService.class);
PendingIntent pendingintent = PendingIntent.getService(context, 0, smsintent, 0);
locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1*60*1000,10,pendingintent);
}
}
So in this receiver i am requesting for location updates using pendingIntent to listen to location change.
My service named SmsService.java looks like:
public class SmsService extends IntentService {
public SmsService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
Location location = (Location) bundle.get(LocationManager.KEY_LOCATION_CHANGED);
Log.d("msg", "Loc is " + location.getLatitude() +","+ location.getLongitude());
sendMessage("983******","msg is " + location.getLatitude()+"," +location.getLongitude());
}
private void sendMessage(String rec,String msg){
//PendingIntent intent = PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0);
SmsManager sms = SmsManager.getDefault();
//Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_LONG).show();
ArrayList<String> parts = sms.divideMessage(msg);
sms.sendMultipartTextMessage(rec,null, parts,null, null);
}
}
My app is not force closing but it is not able to send the message to receiver specified.
Is there any better alternative to this problem.In short i want to implement feature that sends GPS location of User every 5 minutes once user has enabled tracking feature.How can i implement this using services/Intent Services/Broadcast Receivers.?Please help..