How to invoke an activity on SMS receive in Android? - java

My application is receiving an SMS and I want to invoke an activity on receiving that SMS. How do I do this?

create receiver like this
<receiver android:name=".BroadcastServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
</intent-filter>
</receiver>
in class do whatever you want
public class BroadcastServiceReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// do you want
}
}

Related

My FragmentActivity is not receiving broadcasts

I registered a BroadcastReceiver in my FragmentActivity like so:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
setMapLocations(intent);
}
};
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
setMapLocations(intent);
}
};
and start the receiver is onStart():
public void onStart() {
super.onStart();
LocalBroadcastManager.getInstance(this).registerReceiver((mMessageReceiver),
new IntentFilter("MyNotificationData"));
}
and my broadcast sender is defined in the manifest as follows:
<service
android:name=".TrackerMessagingService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
The problem is that onReceive() never gets hit, even though the exact same code worked fine in an AppCompatActivity in the same app. Any idea what's going on? Do broadcast services not play well with FragmentActivity?
I don't know why this worked, but I was able to get around the issue by leaving the BroadcastReceiver code in the AppCompatActivity. In that case, both BroadcastReceivers got hit.

Broadcast class not recieving broadcast intent for BOOT_COMPLETE

I needed to restore alarm after reboot for this I added this broadcast receiver:
public class ClsRestartAlarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Logging.logMessage("Broadcast");
Intent i = new Intent(context, BootService.class);
context.startService(i);
}
}
}
and registered in manifest like this:
<receiver android:name=".classes.ClsRestartAlarm"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and as result of broadcast I'm doing this:
public class BootService extends IntentService {
public BootService() {
super("boot service");
}
#Override
protected void onHandleIntent(Intent intent) {
AlarmManagerUtils.setStartAlarm();
AlarmManagerUtils.setEndAlarm();
}
}
I guess I'm not receiving BOOT_COMPLETE broadcast in ClsRestartAlarm class, because after restart alarm was not set and I was unable to get notification(the starting alarm start a job scheduler for sending notification and end alarm cancels job scheduler)also I have BOOT_COMPLETE permission like this:
<uses-permission android:name="ANDROID.PERMISSION.RECEIVE_BOOT_COMPLETED"/>
Use WakefulBroadcastReceiver instead. This is my workable solution:
public class BRAutoStart extends WakefulBroadcastReceiver {
private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context ctx, Intent intent) {
_A.APPCTX = ctx.getApplicationContext();
if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){
//code
}
}
}
<receiver android:name=".BRAutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Intercept opening android setting of apps with BroadcastReceiver

I declared in AndroidManifest permission and receiver
<uses-permission android:name="android.permission.REAL_GET_TASKS"/>
<receiver android:name=".UninstallIntentReceiver">
<intent-filter android:priority="0">
<action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
<data android:scheme="package" />
</intent-filter>
</receiver>
And here is my java class receiver
public class UninstallIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String[] packageNames = intent.getStringArrayExtra("android.intent.extra.PACKAGES");
if(packageNames!=null){
for(String packageName: packageNames){
Log.d("User selected: ",packageName);
}
}
}
When I open settings of other apps it works well - I get notification
(I can see in logs package name of chosen application), but
when I open settings my own application, I don't receive anything

BroadcastReceiver isn't receiving ACTION_SCREEN_OFF intent

I have created a BroadcastReceiver that should listen for the "ACTION_SCREEN_OFF" intent.
public class ExampleReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// run a service..
}
}
I have registered it in the Manifest:
<receiver android:name=".path.to.ExampleReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SCREEN_OFF"></action>
</intent-filter>
</receiver>
But when the screen is turned off, the onReceive() method is not called. What else do I need to do to get this functionality?
ACTION_SCREEN_OFF is not sent to receivers registered in the manifest. It is only sent to receivers registered via registerReceiver() from a running component.

How to register BroadcastReceiver for sms catcher?

I have been developing the application which must catch all input sms. For it I use SmsReceiver class which extends BroadcastReceiver class and it works, but I need that the application can work after rebooting automatically. For it I use the second BroadcastReceiver RebootingReceiver which extends BroadcastReceiver too, but I don't know how I can register SmsReceiver again. Please, I try to use this code, but it doesn't works:
public class RebootingReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
IntentFilter filter=new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
context.registerReceiver(new SmsReceiver(), filter);
}
}
Where have I made a mistake?
add this in your manifest file
<receiver android:name=".RebootingReceiver">
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

Categories