Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start application without activity, my Broadcast Receiver not work

In my app, I have a Broadcast Receiver for catching the message sent to my phone

<receiver
            android:name="com.qmobile.ows.SMS_Receiver"
            android:enabled="true" 
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />                  
            </intent-filter>
</receiver>

If I start app with activity GUI, the BroadCast Receiver works normally.

I want to start my application without activity and do not show icon app, so I remove this code below from my activity

 <intent-filter>
                  <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>

And after that, Broadcast Receiver do not work.

Please help me solve this problem.

like image 410
CauCuKien Avatar asked Mar 11 '14 06:03

CauCuKien


1 Answers

This is because Android OS does not allow BroadcastReceiver to receive some important broadcast(android.provider.Telephony.SMS_RECEIVED must be one of it) if the app´s process is not alive.It was designed to against the evil apps. If you have an activity running,your process is alive and so your receiver is allowed to receive the broadcast.

I think you can make a transparent activity and use startService to start a service in background,then finish the activity.As your service is running ,your process is alive,so the Android OS will let you to receive the broadcast.

like image 195
tianwei Avatar answered Oct 08 '22 20:10

tianwei