Android : Why does the LocationTracker app in Android 4 Application Development always stops?

on Sunday, September 28, 2014


This is the sample code for the AndroidManifest.xml of the app



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationtracker2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="14" />

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.locationtracker2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" />
<!-- This line will make the APP woken when there is an incoming SMS message -->
<receiver android:name=".SMSReceiver">
<intent-filter android:priority="100">
<action
android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

</application>

</manifest>


And for the SMSReceiver.java class code is



package net.learn2develop.LocationTracker2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;

public class SMSReceiver extends BroadcastReceiver{
LocationManager lm;
LocationListener locationListener;
String senderTel;


public void onReceive(Context context, Intent intent){
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if(bundle != null){
senderTel = "";
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
if(i==0){
senderTel = msgs[i].getOriginatingAddress();
}
str += msgs[i].getMessageBody().toString();
}
if(str.startsWith("Where are you?")){
lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 1000, locationListener);
this.abortBroadcast();
}
}
}

private class MyLocationListener implements LocationListener{

public void onLocationChanged(Location loc){
if(loc != null){
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(senderTel, null, "http://maps.google.com/maps?q=" + loc.getLatitude() + "," + loc.getLongitude(), null, null);
lm.removeUpdates(locationListener);
}
}

public void onProviderDisabled(String provider){
}

public void onProviderEnabled(String provider){
}

public void onStatusChanged(String provider, int status, Bundle extras){
}
}
}


When I try to run this application it always stops. How do I prevent this problem? I have been searching for solutions since forever. I just want to locate using SMS and the network provider. Can you guys suggest other or help me solve this problem. It would be much appreciated


0 comments:

Post a Comment