BroadcastReceiver in service doesn't react - android

I am writing an app which receives SMS data message, encrypt its content and save it to database. To realize it I've created a service with a local BroadcastReceiver as follow:
public class SMMReceiverService extends Service {
private class SMMreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
//call a method from service
}
}
private SMMreceiver smmReceiver;
private IntentFilter intentFilter;
#Override
public void onCreate(){
super.onCreate();
android.os.Debug.waitForDebugger();
smmReceiver = new SMMreceiver();
intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.DATA_SMS_RECEIVED");
intentFilter.addDataScheme("sms");
intentFilter.addDataAuthority("localhost","8901");
registerReceiver(smmReceiver, intentFilter);
}
}
Service is starting normally but onReceive method of SMMreceiver is never called. In manifest I've declared only my service as follow:
<service
android:name=".Services.SMMReceiverService"
android:enabled="true"
android:exported="true" >
</service>
There are also all required permissions:
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
How to solve my problem in correct way? I will be grateful for your help!

Well, it seems like you kind of answered your own question.
"Service is starting normally but onReceive method of SMMreceiver is never called. In manifest I've declared only my service as follow:"
you have to also declare your receiver in your manifest, did you do that?

Related

Android- SMS_RECEIVED + broadcast in a foreground service crash

I have a service that is running in the foreground, inside this service i have a broadcast receiver, that listen to the SMS_RECEIVED action.
When the user is inside the application (both the application and the service are in the foreground) everything works well, and i am receiving the intent.
But when the user exists the application (only the service with the broadcast is in the foreground), the service stops when sms is received.
When the service is stopped no error reports are found anywhere (not in the logcat and no crash dialog pops up).
My service with the broadcast:
public class myService extends IntentService {
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(myService.this, intent.getAction(), Toast.LENGTH_SHORT).show();
}
};
#Override
public void onCreate() {
super.onCreate();
IntentFilter i= new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
i.setPriority(999);
registerReceiver(mReceiver, receiverFilter);
startForeground(...);
}
public void onDestroy() {
super.onDestroy();
stopForeground(true);
}
}
And i also have the following permission in my manifest:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
myService is declared like this in the manifest:
<service
android:name=".Services.myService"
android:enabled="true"
android:exported="false" />
I had the same problem and I solved it removing <uses-permission android:name="android.permission.RECEIVE_SMS" />. But removing this permission I can't detect incoming SMS, so I created a class like these Catching Outgoing SMS using ContentObserver and used MESSAGE_TYPE_RECEIVED = 1 instead MESSAGE_TYPE_SENT = 2. You need to add this permission <uses-permission android:name="android.permission.READ_SMS" />.

Broadcast receiver is not getting invoked

I have used a Broadcast Receiver which is to be invoked when it finds internet Connection on the android device, to check if the Broadcast Receiver is running or not I have tried printing it in the logcat. I have no message being displayed.
I am invoking a service in my broadcast receiver which is also not getting invoked.
I have seen similar questions on stack overflow but still I am unable to solve the issue.
I am stuck with this since many days. Can anybody help me solve this problem?
This is my Broadcast receiver:
public final class ConnectivityReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.e("BRRRRRRRRRR","works!!!! In Broadcast Receiver...!!!");
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
final PendingIntent wakeupIntent = PendingIntent.getService(context, 0,
new Intent(context, LocationUpdate.class), PendingIntent.FLAG_UPDATE_CURRENT);
final boolean hasNetwork = !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if (hasNetwork)
{
// start service now for doing once
context.startService(new Intent(context, LocationUpdate.class));
// schedule service for every 15 minutes
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_FIFTEEN_MINUTES,
AlarmManager.INTERVAL_FIFTEEN_MINUTES, wakeupIntent);
}
else
{
alarmManager.cancel(wakeupIntent);
}
}
}
Manifest.xml
<receiver android:name="info.androidhive.loginandregistration.ConnectivityReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
Since you are checking connectivity changes, i suggest you can first check if your app has permissions to connect to the internet.
The permission for the app to access the internet would be -
<uses-permission android:name="android.permission.INTERNET" />
Your receiver looks fine. Please, check if your package name is correct. Registering receiver in AndroidManifest.xml should me sufficient, but if it doesn't work for some reason, you can try different solution.
Try to register your receiver programmatically in the following way e.g. in onResume() method in your activity:
#Override
protected void onResume() {
super.onResume();
ConnectivityReceiver receiver = new ConnectivityReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(receiver, filter);
}
You should also unregister your receiver in onPause() method in the activity:
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
If it won't help, you can add new permissions to AndroidManifest.xml file inside the tag.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Monitoring network connection type change in Android Service

I want to capture the network connection type change in Android Service and run a code of this Service when the event fires. How to do that inside a Service class only without separate class? I have this code, but it doesn't work.
BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
playlist="NETWORK TYPE CHANGED";
}
};
public void playSong(Context c, String url) {
try
{
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);
Your BroadcastReceiver needs to receive the Intent from the system, so you need to register your BroadcastReceiver in your AndroidManifest
<receiver
android:name=".ConnectivityReceiver
android:exported="false" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
and do not forget the permission
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

How custom permissions are used to restrict sending and receiving the broadcasts within a single app?

I am learning from "Learning Android" - Oreilly - Marko Gargenta.
I am at chapter 11 (Broadcast Receivers)
I followed the book and everything work OK. But I have a question about how custom permissions are used to restrict sending and receiving the broadcasts within a single app.
The book is clear about this topic. but I feel there is something missing.
How do the receiver and the sender tell each other about different permissions?
In AndroidManifest.xml file:
<permission
android:name="saleh.yamba.SEND_TIMELINE_NOTIFICATIONS"
android:description="#string/send_timeline_notifications_permission_description"
android:label="#string/send_timeline_notifications_permission_label"
android:permissionGroup="android.permission-group.PERSONAL_INFO"
android:protectionLevel="normal" />
<permission
android:name="saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONS"
android:description="#string/receive_timeline_notifications_permission_description"
android:label="#string/receive_timeline_notifications_permission_label"
android:permissionGroup="android.permission-group.PERSONAL_INFO"
android:protectionLevel="normal" />
<uses-permission android:name="saleh.yamba.SEND_TIMELINE_NOTIFICATIONS" />
<uses-permission android:name="saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONS" />
In the Service that send broadcasts:
Intent intent = new Intent("saleh.yamba.NEW_STATUS");
updaterService.sendBroadcast(intent, "saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONS");
Here, sender sends intent with saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONS permission, Ok, How does the receiver know about this permission?
In the Activity that receives the broadcast via BroadcastReceiver:
TimelineReceiver receiver;
IntentFilter filter;
protected void onCreate(Bundle savedInstanceState)
{
receiver = new TimelineReceiver();
filter = new IntentFilter("saleh.yamba.NEW_STATUS");
}
protected void onResume()
{
this.registerReceiver(receiver, filter, "saleh.yamba.SEND_TIMELINE_NOTIFICATIONS", null);
}
protected void onPause()
{
this.unregisterReceiver(receiver);
}
private class TimelineReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
//do something.
}
}
Here receiver receives it with another permission. OK,
How does the receiver know about saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONS.
There is nothing in the code of the receiver part that tells that the BroadcastReceiver will be invoked only if the receiver has saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONSpermission.
There is nothing in the code of the receiver part that tells that the BroadcastReceiver will be invoked only if the sender has saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONSpermission.
Yes, there is. You passed in saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONS to registerReceiver().
Specifically, you are using the four-parameter version of registerReceiver(), where the third parameter is a "String naming a permissions that a broadcaster must hold in order to send an Intent to you. If null, no permission is required."

restart force stopped app on receiving sms

I am trying to restart my app on receiving sms if it is force stopped. This is my code.
Its not restarting the app.Should I try writing receiver as another class.
In manifest :
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<receiver android:name=".MySMSbr">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
My mainActivity onCreate() :
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
BroadcastReceiver SMSbr;
public void onCreate(Bundle savedInstanceState) {
Toast.makeText(getApplicationContext(),"in OnCreate", Toast.LENGTH_LONG).show();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SMSbr = new BroadcastReceiver()
{
#Override
public void onReceive(Context context,Intent intent)
{
this.abortBroadcast();
Toast.makeText(context, "in onReceive", Toast.LENGTH_LONG).show();
toggleLogging(AppSettings.getServiceRunning(MainActivity.this),
AppSettings.getLoggingInterval(MainActivity.this));
this.clearAbortBroadcast();
}//end of onReceive method
};//end of BroadcastReceiver
IntentFilter SMSfilter = new IntentFilter(SMS_RECEIVED);
this.registerReceiver(SMSbr, SMSfilter);
}
in togglelogging the service is started
where is it going wrong.
You are declaring a BroadcastReceiver in your manifest - i.e, static receiver but in fact you do not have such a class and you are creating a dynamic receiver in your activity.
What you are actually doing is registering your receiver when your activity starts, but you want the other way around (start your activity/app once the receiver receives a broadcast).
You need to create a class which is called SMSbr extends BroadcastReceiver and there you can perform your logics.
That way you will have the receiver always registered and when an SMS broadcast will be received it will wake your app.

Categories

Resources