システム起動時に起動するレシーバー
public class BootBroadcastReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context,SmsReceiver.class);
context.startActivity(i);
}
}
着信 SMS をリッスンするレシーバー
public class SmsReceiver extends BroadcastReceiver {
private String KEYWORD="GPS";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
Bundle bundle = intent.getExtras();
if (bundle != null)
{
//Receiving messages
try{
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
/*Checking for a key word*/
if (smsMessage[0].getMessageBody().equalsIgnoreCase(KEYWORD))
{
Intent in = new Intent(context, Service.class);
context.startService(in);
}
}catch (Exception e){
Log.d("ERROR", e.getMessage());
}
}
}
}
}
package edu.na.aggas.comGpsService;
SMSにキーワードが現れると起動するサービス
public class Service extends android.app.Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
LocationManager locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locListener = new mylocationListener();
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
disableGps();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
enableGps();
}
public void enableGps(){
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
}
public void disableGps(){
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
}
/*Inner class*/
class mylocationListener implements LocationListener{
/*If the location changes get new coordinates*/
@Override
public void onLocationChanged(Location location) {
if(location != null)
{
double plong = location.getLongitude();
double plat = location.getAltitude();
Toast.makeText(getBaseContext(), "Longitude: " + plong + "Latitude: " + plat, Toast.LENGTH_LONG).show();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status,Bundle extras) {
}
}
}
Xml
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service android:name=".Service">
<intent-filter>
<action android:name = "edu.na.aggas.comGpsService.Service">
</action>
</intent-filter>
</service>
<receiver android:name=".BootBroadcastReciever">
<intent-filter>
<action android:name ="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
問題がどこにあり、どのように解決するか教えてください。