-1

在系统启动时启动的接收器

公共类 BootBroadcastReciever 扩展 BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Intent i = new Intent(context,SmsReceiver.class);
    context.startActivity(i);
}

}

收听传入短信的接收者

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;

当关键字出现在短信中时启动的服务

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>

谁能告诉我问题出在哪里以及如何解决......?

4

3 回答 3

0

您必须将消息发送到 BroadcastReciever 所以

用这个

@Override
public void onReceive(Context context, Intent intent) {

    Intent i = new Intent(context,SmsReceiver.class);
    context.sendBroadcast(i);

}
于 2012-09-05T08:50:30.290 回答
-1

如果您可以从 LogCat 添加日志,我们可以为您提供更好的帮助。但是首先,您是否在清单文件中添加了以下代码:

<receiver android:name="BootBroadcastReciever" >

    </receiver>
于 2012-09-05T08:51:05.203 回答
-1

Toast.makeText(this, "服务创建", Toast.LENGTH_LONG).show();

您不能从服务中显示 Toast,要显示 Toast,您需要有活动上下文。因此,注释掉所有 Toast 语句并尝试。

于 2012-09-05T08:53:55.500 回答