开发者

Broadcast Receiver is not starting Service

开发者 https://www.devze.com 2023-02-16 19:43 出处:网络
Thank you in advance for all your help.I am still very new with Android and in the programming world, and was looking for a little guidance.I\'m trying to have a text message get listened to, and then

Thank you in advance for all your help. I am still very new with Android and in the programming world, and was looking for a little guidance. I'm trying to have a text message get listened to, and then kick start a service to run in the background. I know the Broadcast Receiver is working because I have a Toast notification pop up when I send the emulator a text, but that's it. I don't receive anything after that from the Toast that I have in the service.

Here is my Broadcast Receiver

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle; 
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {
protected String className = this.getClass().getName();
protected int toastLength = Toast.LENGTH_SHORT;

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();

    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]);
    }

    // show first message
    Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
    toast.show();
    Intent serviceIntent = new Intent(context, MyService.class);
    serviceIntent.setAction("SMS.example.MyService");
    context.startService(serviceIntent);
    }
}

I get the toast notification to pop up, but it does not start the service after a text is received. Here is the code for the service:

public class MyService extends Service {
final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
BroadcastReceiver SMSReceiver = new BroadcastReceiver(){
protected String className = this.getClass().getName();
protected int toastLength = Toast.LENGTH_SHORT;    

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, className, toastLength).show();
        Log.i("INCOMINGSMSRECEIVER", intent.getAction());

        if (intent.getAction().equals(SMS_RECEIVED)) {
            SmsManager sms = SmsManager.getDefault();

            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i=0; i<pdus.length; i++)
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

                for (SmsMessage message : messages) {
                    String msg = message.getMessageBody();
                    String to = message.getOriginatingAddress();
                    //sms.sendTextMessage(to, null, "Got It" + msg, null, null);
                    Log.i("INCOMINGSMSRECEIVER", "SMS received."); 

                }
            }
        }           
    }
};

    //@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate();

    Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
    IntentFilter filter = new IntentFilter(SMS_RECEIVED);       
    registerReceiver(SMSReceiver, filter);

}
}

Sorry for all the code, just want to give you guys all the information. Lastly here is the Mainfest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="SMS.example"
  android:versionCode="1"
  android:versionName="1.0">
   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
   <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
   <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
   <uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">    
<receiver android:name=".MyBroadcastReceiver" android:enabled="true">
    <intent-filter>
              <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter>
</receiver> 
<service android:name=".MyService" android:enabled="true">
    <intent-filte开发者_开发百科r>
      <action android:name="SMS.example.MyService" />    
    </intent-filter>
</service>    

Can anyone please tell me why my service is not being started?


i think this is because you don't use onStartCommand() read this : http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int)

be notified that onStart() is deprecated and onStartCommand() should be used instead.


I thought I would keep this code up for others, but I figured out the problem. I didn't have the toast popping up for the onCreate(), but the onStart() method will pop up a toast! Hope this helps others.

0

精彩评论

暂无评论...
验证码 换一张
取 消