Static Broadcast Receiver not being called for SMS_RECEIVED - android

i am writing a broadcastreciever to listen for SMS_RECEIVED action .it works perfectly fine if i register it dynamically but if i register it statically in the Manifest file the broadcast receiver is not being called.
<receiver android:name=".MyReceiverBroadcast"><intent-filter>
<action android:name="android.provider.Telephony.SMS_RECIEVED"></action>
</intent-filter></receiver>
My broadcast reciever is as follows
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if(action.equals(SMS_RECEIVED_ACTION))
{
Log.v("sms", "reciever called");
Object[] messages=(Object[])intent.getExtras().get("pdus");
for(Object message:messages)
{
byte[] messagedata=(byte[])message;
SmsMessage smsmessage=SmsMessage.createFromPdu(messagedata);
processmessage(smsmessage,context);
}
}
}
private void processmessage(SmsMessage smsmessage,Context c) {
String from=smsmessage.getOriginatingAddress();
String messcontent=smsmessage.getMessageBody();
Toast.makeText(c,"from ="+from+" and mess="+messcontent,Toast.LENGTH_LONG).show();
Log.v("sms","from ="+from+" and mess="+messcontent);
}
My Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tech.myfirst" >
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
<activity
android:name=".SmsReceiverDemo"
android:label="SmsReciever Demo"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.tech.myfirst.MyReceiverBroadcast"><intent-filter>
<action android:name="android.provider.Telephony.SMS_RECIEVED"></action>
</intent-filter></receiver>
</application>
</manifest>
I have added all the necessary permissions.
What i am missing here.
i am running the program on emulator nexus 5 api 21.
thanks

Assuming everything else is correct, it appears the problem is that you've misspelled RECEIVED in the <intent-filter>'s <action> for the <receiver> entry in the manifest. It should be:
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
You might also want to check the value of the SMS_RECEIVED_ACTION String.

I've had this problem before, and was solved by changing the attribute
android:name=".MyReceiverBroadcast"
To
android:name="my.package.name.MyReceiverBroadcast"
I don't know why this is happening, but it worked for me.

Related

Battery broadcast receiver declared in manifest doesn't work

I want to receive broadcast BATTERY_LOW, so I declared broadcast receiver in manifest (in order not to rely on current activity. I want my application receive this broadcast even when it's not running. Here's what I did.
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver android:name=".BatteryLevelReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
<action android:name="android.intent.action.BATTERY_OKAY"/>
<action android:name="android.intent.action.POWER_CONNECTED"/>
<action android:name="android.intent.action.POWER_DISCONNECTED"/>A
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
And BatteryLevelReceiver itself (package com.wayruha.serviceguard.onduty) :
public class BatteryLevelReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.w(this.getClass().getName(),intent.getAction());
Log.w(this.getClass().getName(),"Low level:"+intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1));
}
}
I cant receive any of declared events!There are some similar questions here, but none of them helps me. Im using telnet to set capacity or 'ac on/off' for an emulator.
Try to add permission in your manifest file:
<uses-permission android:name="android.permission.BATTERY_STATS"/>

receive sms broadcast after clear ram

i know this question may be asked by another user ...but i can not solve my problem
i create one broadcast for receive sms and it worked well but when user clear ram broadcast does not work ...
how i can create a broadcast that work even user clear ram
it is my code
public class ReceiveSms extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("log","sms received");
// Toast.makeText(App.context,"you have sms",Toast.LENGTH_SHORT).show();
Object[] pdus= (Object[]) intent.getExtras().get("pdus");
SmsMessage sms=SmsMessage.createFromPdu((byte[]) pdus[0]);
String body=sms.getMessageBody();
String sender=sms.getDisplayOriginatingAddress();
Log.i("log","sms body"+body);
Toast.makeText(App.context,"message from :"+sender,Toast.LENGTH_SHORT).show();
Intent startProgram=new Intent(App.context,MainActivity.class);
startProgram.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
App.context.startActivity(startProgram);
}
}
and mainfast
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.masiha68.sms">
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:name=".App"
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>
<service android:name=".RC"
android:process=":remote"
android:enabled="true"
>
</service>
<receiver android:name=".ReceiveSms"
android:process=":remote"
>
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED">
</action>
</intent-filter>
</receiver>
</application>
</manifest>
In huawei device, you need to enable you app in 'Protected apps' to keep running after the screen off or after you kill app from background.
For that goto,
Phone Manager -> Power saving -> Protected apps -> find your app and 'enable' it.

Android receiver works dynamically but not via manifest file

I have a broadcast receiver that works when I set it up dynamically, but does not work when declared in a manifest file.
I've googled for this and all the examples match what I'm doing in my code. I do have an activity in my manifest file, so that can't be the problem. I've tried fully specifying the receiver class name as well as using .MyBroadcastReceiver but that did not make any difference.
Can someone help?
Here is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mikrodyne.receiverdemo" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.mikrodyne.receiverdemo.MyBroadcastReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG" />
</intent-filter>
</receiver>
</application>
</manifest>
And here is my receiver class
public class MyBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
Log.d("Receiver", "Received action: " + action);
}
}
TIA
Nisha Miller
As per the documentation for HEADSET_PLUG:
You cannot receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver().
So what you're trying to do is not possible.
If you want to get events from outside, you need to set android:exported="true"
Note: you can't get that intent.
Put this in your AndroidManifest.xml
<receiver
android:name="com.mikrodyne.receiverdemo.MyBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG" />
</intent-filter>
</receiver>

Issue with automatically starting app

I see this has been asked quite a bit, but I can't seem to resolve my problem with what is out there.
My onReceive() method in broadcast receiver isn't being called.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.app.test.TestActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
BootUpReceiver.java
package com.app.test;
public class BootUpReceiver extends BroadcastReceiver {
private static final String TAG = "TESTAPP_BootUpReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "helllllllllllllllo");
Toast.makeText(context, "boot completed received", Toast.LENGTH_LONG).show();
// Intent i = new Intent(context, TestActivity.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(i);
}
}
Have tried using the entire path instead of .BootUpReceiver, didn't work. Not seeing anything from logcat or any Toast messages. Going into adb shell and emitting the boot_completed event that way doesn't help as the device then reboots.
Is there anything I am doing wrong? I read something about applications being inactive when device boots, does that affect my problem?
Here are some reference in Android developer website
http://developer.android.com/guide/topics/data/install-location.html
Broadcast Receivers listening for "boot completed"
The system delivers the ACTION_BOOT_COMPLETED broadcast before the external storage is mounted to the device. If your application is installed on the external storage, it can never receive this broadcast.

Android broadcast receiver not working when trying to receive bootcomplete or screen off

So I am trying to develop a custom lockscreen
but my broadcastreceiver won't fire
my manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.alexander.fuchs.lockscreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".app"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".myreceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
my receiver :
public class myreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("receiver","works");
Toast.makeText(context,"works",Toast.LENGTH_LONG).show();
Intent in=new Intent(context,app.class);
context.startActivity(in);
}
}
the receiver should show me that it is fired :D
but ther aren't any logs in logcat
well , for the screen off and screen on , this cannot be inside the manifest , but only at runtime . see this:
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
for the bootup , it must be in the manifest , so something else is wrong with it.check the path of the class. this is surely the cause of the problem.

Categories

Resources