I am trying to build a simple app that will wait until a new SMS arrives, extract and process the data from it. The application should run in the background. The GUI has a single switch element to start/stop the broadcast receiver. Even if the app is destroyed and the screen is locked, the app should still be working unless the user manually turns it off.
I searched every resource on stackoverflow, and most of them did it this way, yet, it still does not work for me, and I can't seem to know why. I know because Log.d(...)
is not returning anything. Any help would be appreciated.
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("RECEIVER", "ENTERED");
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Log.d("RECEIVER", "SMS RECEIVED");
SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
for(int i = 0; i < 4; i++){
SmsMessage sms = messages[i];
Log.d("Message " + i + 1 + " from: ", sms.getOriginatingAddress());
Toast.makeText(context,"SMS from " + sms.getOriginatingAddress(), Toast.LENGTH_SHORT).show();
}
}
}
}
public class MainActivity extends AppCompatActivity {
IntentFilter filter;
SMSReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
receiver = new SMSReceiver();
Switch startSwitch = (Switch) findViewById(R.id.startSwitch);
startSwitch.setChecked(false);
startSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
getApplication().registerReceiver(new SMSReceiver(), filter);
} else {
getApplication().unregisterReceiver(receiver);
}
}
});
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ahmad.smsforwarder">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
</manifest>
Creating a BroadcastReceiverThe onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context. startActivity(myIntent); or context.
Definition. A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.
When you Register for any Broadcast receiver by Default onReceive() method will be called on the Main Thread( UI Thread). But If we want to do any heavy operations or if your broadcast is calling multiple times, then so much load will be accumulated on Main Thread which is not recommended.
You have to register your receiver in AndroidManifest.xml with specific intent in intent filter.
<receiver
android:name=".SMSReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
I suffered too long to find out that as of Android 6.0 (API 23), you will have to register permissions in the main activity java class as well. I fixed the problem by adding the following lines to the main activity's class onCreate() Method:
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_SMS},1);
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECEIVE_SMS},1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With