Android - Unable to register broadcast from a service
<p>I don't have much experience in Android dev. Right now, i am unable to register a receiver from the onStartCommand method of a service. </p>

<p>In english it would be : I have two buttons (Start/Stop service) on an Activity. When I click on the Start button, I wan't to run the service which will register the BroadcastReceiver (SMS_RECEIVED). When a SMS is received, I wan't to see a log trace. But I don't see it !</p>

<p>It seems that I can't register my Broadcast receiver but I know that my Service is running (thanks to the logs).</p>

<p>In addition, I'd like to make my service, and consequently my BroadcastReceiver, persistent (if I quit the application, I want it to run in background, and even if I restart the phone).</p>

<p>Can anyone tell me what's wrong in my code ? ... and maybe give me help with my second question .... :)</p>

<p>Thanks !</p>

<p>Here is my code :</p>

<p>--- Activity : Main ---</p>

<pre><code>package com.tuto.servicerunbroadcastreceiver;

import XYZ

public class Main extends Activity implements OnClickListener
{
 Button bt_start;
 Button bt_stop;

 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  bt_start = (Button)findViewById(R.id.button1);
  bt_start.setOnClickListener(this);
  bt_stop = (Button)findViewById(R.id.button2);
  bt_stop.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) 
 {  
  switch (v.getId())
  {
   case R.id.button1 :
   {
    Log.d("Button : ", "Button start");
    startService(new Intent(this, svcMessage.class));    
    break ;
   }
   case R.id.button2 :
   {
    Log.d("Button : ", "Button stop");
    stopService(new Intent(this, svcMessage.class));
    break ;
   }
  }
 }  

 @Override
 public boolean onCreateOptionsMenu(Menu menu) 
 {
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }
}
</code></pre>

<p>--- Service : svcMessage ---</p>

<pre><code>package com.tuto.servicerunbroadcastreceiver;

import XYZ;

public class svcMessage extends Service 
{
 private static final String ACTION_RECEIVE_SMS =    "android.provider.Telephony.SMS_RECEIVED";
 private BroadcastReceiver br_receiver;

 @Override
 public IBinder onBind(Intent arg0) 
 {
  return null;
 }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId)
 {
  final IntentFilter filter = new IntentFilter();
  filter.addAction("ACTION_RECEIVE_SMS");
  Log.d("Service : ", "start");
  this.br_receiver = new BroadcastReceiver() 
  {
   @Override
   public void onReceive(Context context, Intent intent) 
   {
    Log.d("LOG : ", "onReceive");
   }
  };
  this.registerReceiver(this.br_receiver, filter);  
  return (START_STICKY);
 }

 @Override
 public void onDestroy()
 {
  super.onDestroy();
  Log.d("Service : ", "destroy");
  this.unregisterReceiver(this.br_receiver);
 }  
}
</code></pre>

<p>--- Manifest.xml ---</p>

<pre><code>&lt;manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tuto.servicerunbroadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" &gt;

    &lt;uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" /&gt;

    &lt;application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" &gt;
        &lt;activity
            android:name="com.tuto.servicerunbroadcastreceiver.Main"
            android:label="@string/app_name" &gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;
                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
        &lt;service android:name="com.tuto.servicerunbroadcastreceiver.svcMessage"&gt;
        &lt;/service&gt;
    &lt;/application&gt;
&lt;/manifest&gt;
</code></pre>
