Android receiver works dynamically but not via manifest file - android

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>

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"/>

Broadcastreceiver not working after boot

I have created an apps that will receive notifications from firebase thus hoping to start the app service after user boot their phone so that they do not need to manually start the apps again. However, the broadcast receiver seems to just not working.
AndroidManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.simplifiedcoding.firebasecloudmessaging">
<!-- Adding Internet Permission -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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>
<!--
Defining Services
-->
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name=".NotificationService"/>
<receiver android:name=".Broadcast" android:exported="true" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Broadcast.java (BroadcastReceiver)
public class Broadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
Intent service = new Intent(context, NotificationService.class);
context.startService(service);
Toast.makeText(context, "Broadcast started", Toast.LENGTH_LONG).show();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have checked the followings
Permission RECEIVE_BOOT_COMPLETED declared and not within tag
receiver tag is being written correctly
Am I doing wrong or still missing something else? Do I have to call it at mainAcitivity which I doubt I should ? Any guidance are much appreciated.
Got the solution and it was a very dumb reason.
I'm using Oppo's phone to test and Oppo has its own Security Manager App where you have to manually allow specific apps to be start up automatically after boot. That's all.
I suspect most android phones has this feature as well thus bear in mind to check whether there is such app and if it does then remind the user to allow the apps in the Security Manager App before they start rebooting their phone and not able to use any service's your App intended to provide.
Hope this helps!
Try this, it worked for me.
<receiver android:enabled="true" android:name=".Broadcast"
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>

Static Broadcast Receiver not being called for SMS_RECEIVED

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.

How to register a custom Intent filter to a broadcast receiver in AndroidManifest.xml?

I have defined a receiver in AndroidManifest.xml to receive a PlAY_FINISHED action, and in other file I send an intent to that broadcast receiver like follows:
public String PlAY_FINISHED = "play finished";
...
Intent in = new Intent(PlAY_FINISHED);
this.service.sendBroadcast(in);
so in my manifest file, i set it like this, where MyStaticString is a class that contains all the static string in the application. Is this the correct way?
<intent-filter>
<action android:name="com.mysite.appname.MyStaticString.PLAY_FINISHED" />
</intent-filter>
The android:name of an intent filter in the manifest is just an arbitrary string, not the "path" to a Java constant. The problem is that your string constant in code is defined as "play finished", which doesn't match the name "com.mysite.appname.MyStaticString.PLAY_FINISHED" that you've specified in the manifest.
It should be
public String PlAY_FINISHED = "com.mysite.appname.MyStaticString.PLAY_FINISHED";
It doesn't matter what the variable is called, or even if you store the string in a variable at all. Or that its name contains a typo :)
You could instead change the android:name in the manifest to "play finished", but custom broadcast actions are system-wide so they should be qualified with the package name of your app to avoid collisions with other apps.
Registering in Android Manifest file.
<receiver android:name=".ReceiverDemo">
<intent-filter>
<action android:name="marakana.intent.action.ReceiverDemo" />
</intent-filter>
</receiver>
Registering programmatically.
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
// Create the receiver
receiver = new TimelineReceiver();
filter = new IntentFilter( UpdaterService.NEW_STATUS_INTENT );
}
protected void onResume() {
super.onResume();
super.registerReceiver(receiver, filter,
"com.marakana.yamba.SEND_TIMELINE_NOTIFICATIONS", null);
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
...
UPDATE: Multiple values
If more than one value can be specified, the element is almost always repeated, rather than listing multiple values within a single element. For example, an intent filter can list several actions:
<intent-filter . . . >
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.INSERT" />
<action android:name="android.intent.action.DELETE" />
. . .
</intent-filter>
UPDATE2: That's an example of AndroidManifest.xml
<manifest
package="com.marakana.android.lifecycle"
android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="11" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<application
android:name=".ApplicationDemo"
android:icon="#drawable/icon"
android:label="#string/app_name">
<activity
android:name=".ActivityDemo"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AnotherActivity"></activity>
<activity android:name=".SystemServicesDemo"></activity>
<service android:name=".ServiceDemo"></service>
<service android:name=".IntentServiceDemo">
<intent-filter>
<action android:name="marakana.intent.action.IntentServiceDemo" />
</intent-filter>
</service>
<receiver android:name=".ReceiverDemo">
<intent-filter>
<action android:name="marakana.intent.action.ReceiverDemo" />
</intent-filter>
</receiver>
<provider
android:name=".ProviderDemo"
android:authorities="com.marakana.android.lifecycle.providerdemo" />
</application>
</manifest>

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