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.
Broadcast SMS Receiver
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();
}
}
}
}
Main Activity
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
<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>