I have implemented simple message(xmpp protocol) application using smack api.there are number activities which are receiving notification when incoming message comes.now i want to notify my application when the incoming message comes even the application is not running.By reading documentation i know that this is achieved by Broadcast receiver.But how to adopt this to my application.
You don't need the broadcast receiver to do this.
In you androidmanifest.xml for eg: consider this activity.
<activity android:name=".activity.message"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleInstance">
<intent-filter>
<action android:value="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</activity>
Once you add the intent to the activity's intent filter list in android.xml,whenever that intent is thrown your activity will be invoked irrespective of whether it is open or not.
Here the intent you would want to add to listen for incoming messages would be
<action android:value="android.provider.Telephony.SMS_RECEIVED" />
I think you also need to have permissions for this so add this also to your manifest.xml
<uses-permission id="android.permission.RECEIVE_SMS" />
Related
I know that I can start activity on boot by calling it from a BroadcastReceiver but what if I wanted to do the following:
<activity
android:name="MyActivity"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</activity>
is this possible? and how?
is this possible?
No.
First, android.intent.action.BOOT_COMPLETED is a broadcast Intent. You cannot respond to it via an <activity> or <service> manifest element.
Second, android:permission="android.permission.RECEIVE_BOOT_COMPLETED will prevent anything from starting your activity, unless it also holds RECEIVE_BOOT_COMPLETED. That is not how you usually use that particular permission.
I'm trying to receive a simple custom intent, based on my wn URI.
Here is my manifest:
<uses-sdk
android:minSdkVersion="14"
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.example.intenttest.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.example.intenttest.TestReceive"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="testo" />
</intent-filter>
</receiver>
</application>
My receiver is extremely simple:
public class TestReceive extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
Log.d("", "YAY!!!!!");
Toast.makeText(context, "TEST!!!", Toast.LENGTH_LONG).show();
}
}
When I try browsing to testo://blahblah, or fir this intent via URI Launer my receiver is not being fired.
Here is the code to simulate firing the intent from a different app:
String url = "testo://test/test";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
sendBroadcast( i );
But when I move the <intent-filer> block to the <activity> tag in the manifest, the activity IS being fired.
How can I make my receiver receive the intent?
If you are trying to dynamically register a receiver you should do so programmatically. This example by Eric Nordvik will server you well!
Now, the main reason to do it this way is because it is straight forward. The downside is that you won't be able to receive broadcasts when your application's lifecycle is not currently active (paused, stopped).
Update: As read in the comments, the OP requires to receive broadcasts independent of the lifecycle of the activity. I would urge anyone to rethink his design and only then decide if it is really needed.
As specified by the official documentation, android:name is used to designate the name of the class that implements the BroadcastReceiver. Omitting it in your code means that the Activity itself will receive the broadcast. I believe your issue is that you did not fully qualify it by android:name but referred to it as name. Fixing it should also fix your problem!
Update: The OP has since corrected a typo and this is not the issue. At this point, my best guess is either that the broadcast is generated wrongly or that the reference to the BroadcastReceiver should use the shorthand notation android:name=".NameOfReceiverClass".
OK - This whole issue was probably caused by me taking the wrong approach.
The sample code for sending the intent DOES work and does initiate my receiver.
So it appears that most QR Readers in the market do not fire an intent with the QR-read URI, as in my sample code, but rather look for an Activity that should respond to it and then call it directly.
I have no idea why is this the case.
The solution was to create a silent Activity, that handles what the BroadcastReceiver was supposed to handle.
Oy vey.
I have some receivers declared in my AndroidManifest :
<!-- no warning -->
<receiver
android:name=".receivers.TriggerMonitoringBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<!-- no warning -->
<receiver
android:name=".receivers.ScanResultsReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.net.wifi.SCAN_RESULTS" />
</intent-filter>
</receiver>
<!-- warning : Exported receiver does not require permission-->
<receiver
android:name=".receivers.BatteryMonitoringReceiver"
android:enabled="false">
<intent-filter>
<action android:name="#string/intent_action_setup_alarm" />
<action android:name="#string/intent_action_cancel_alarm" />
<action android:name="#string/intent_action_monitor" />
</intent-filter>
</receiver>
The first one is meant to receive a BOOT_COMPLETED action. The second is meant to receive android.net.wifi.SCAN_RESULTS. The third one is meant to receive some actions I broadcast (intent_action_monitor) and some actions broadcasted by the AlarmManager (intent_action_setup_alarm etc).
Two questions:
Why don't I get the warning on all receivers?
What permissions do I need to set for receivers meant to receive from system services to correct the warning (I understand what it is about and I don't want anyone to use my receivers anyway) ? Will exported="false" do for boot receivers, wifi receivers, alarm receivers etc?
I thought of using a custom permission with android:protectionLevel="signatureOrSystem" but the docs advise against both this protection level and custom permissions. So how I should handle this warning ?
Links to the docs and/or some code will be much appreciated.
Why don't I get the warning on all receivers ?
Because the first two are clearly designed to be broadcast by Android. The last one is unknown, partly because you did not supply the string resource values, and possibly because they are your own unique action strings.
What permissions do I need to set for receivers meant to receive from system services to correct the warning
The correct solution is to delete the <intent-filter>. If you are broadcasting these Intents, or if you are wrapping an Intent in a getBroadcast() PendingIntent, you do not need action strings. Use the Intent constructor that takes the Java class object as the second parameter, and use that:
new Intent(this, BatteryMonitoringReceiver.class)
You are welcome to still attach an action string to that Intent, if you want, but you can dump the <intent-filter> (routing will be based on the supplied component, in this case the Java class).
Only use an <intent-filter> when you are expecting the OS or third-party apps to initiate the Intent themselves (executing a PendingIntent that you created does not count).
The warning "Exported receiver does not require permission" means, You have an intent-filter with some action (which means by default you have android:exported="true" set and it can now receive broadcasts from ANY broadcasters outside of your application) Since it can receive broadcasts from ANY broadcasters outside of your application, it warns you by saying "Hey, are you sure ANY broadcaster can invoke you? In my opinion, it is better if you allow only those broadcasters to invoke you that has the permission you have set for this receiver through android:permission"
You can remove this warning by adding android:exported="false" to the receiver tag
If you do want to export your receiver to other processes, you can add your own permission definition in your android-manifest file for avoiding this warning, like
<permission
android:name="com.yourpage.permission.YOUR_PERMISSION"
android:protectionLevel="normal" />
<uses-permission
android:name="com.yourpage.permission.YOUR_PERMISSION" />
<receiver <!-- warning : Exported receiver does not require permission-->
android:name=".receivers.BatteryMonitoringReceiver"
android:permission="com.yourpage.permission.YOUR_PERMISSION"
android:enabled="false" >
<intent-filter>
<action android:name="#string/intent_action_setup_alarm" />
<action android:name="#string/intent_action_cancel_alarm" />
<action android:name="#string/intent_action_monitor" />
</intent-filter>
</receiver>
for more information, you can refer to http://developer.android.com/training/articles/security-tips.html
If, like me, you are here because your app built with a previous SDK version stopped working with more recent versions and you would like to fix it with minimal change, just add
android:exported=false
to the receiver tag in the manifest file. The solution by CommonsWare is obviously the one to go with for the long term but this fixes the issue temporarily if you are using custom intents and don't mean to export them.
Going by Lubo's way, you would need to export this custom permission, which would prompt the user before installation. That means the descriptive text for the permission needs to be well written so you don't end up scaring the user into changing his mind about installing the app. Also, it would need to be translated into all your target languages.
To hide this warning, add tools:ignore="ExportedReceiver" to the receiver:
<receiver
android:name=".MyReceiverIndentedForOtherAppsWithoutPermissions"
tools:ignore="ExportedReceiver">
<intent-filter>
<action android:name="com.my.app.CUSTOM_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
I am trying to write a simple application to interact with NFC tags, but I cant seem to get my phone to do anything but trigger the default NFC tag app. I really just want to be able to intercept any tag I scan, determine if it has some data on it, and take action accordingly.
Right now my manifest file looks like
<uses-sdk android:minSdkVersion="10" />
<uses-feature android:name="android.hardware.nfc" android:required="true"/>
<uses-permission android:name="android.permission.NFC"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".NfcActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
</intent-filter>
</activity>
</application>
However when scanning an NFC tag, I never see the activity start. Am I missing something here? I tried placing the intent filter inside a BroadcastReceiver and had no luck either...
You cannot have your app started by all NFC tags you scan. Android will determine what the most suitable app is based on how specific the intent filter is. However, if your app is running in the foreground, you can use NFC foreground dispatch to catch all NFC intents.
In onCreate() add:
mAdapter = NfcAdapter.getDefaultAdapter(this);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
In onResume() add:
mAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
In onPause() add:
mAdapter.disableForegroundDispatch(this);
In onNewIntent you can get at the NFC tag like this:
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
The SDK docs show this as a basic example.
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
You are expecting the tags to be defined in NDEF format. So your program will only be launched if the tags being read is in NDEF format.
You can try more generic intent filters like TAG_DISCOVERED or TECH_DISCOVERED.
Android automatically choses the most relevant application to handle the scanned NFC tag. You need to be more specific in your intent-filter, i.e. only listen for TEXT-Tags, URL-Tags, or CONTACT-Tags. This can be done by further specifying the filter, using, your example, <data android:mimeType="text/plain" /> for TEXT-Tags. Otherwise, the default NFC-Tag app will be triggered.
I'm currently trying to catch states for ACTION_SCO_AUDIO_STATE_CHANGED as specified by:
http://developer.android.com/reference/android/media/AudioManager.html#ACTION_SCO_AUDIO_STATE_CHANGED
I have registered the intent on my manifest but I'm not getting anything when connecting to a bluetooth device. Any particular permission I need or something?
Manifest:
<receiver android:name="com.app.receiver.BluetoothReceiver">
<intent-filter>
<action android:name="android.media.SCO_AUDIO_STATE_CHANGED" />
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
<action android:name="android.bluetooth.device.action.BOND_STATE_CHANGED" />
</intent-filter>
</receiver>
I can't get any of the calls from BOND_STATE_CHANGED, SCO_AUDIO_STATE_CHANGED or ACL_DISCONNECT_REQUESTED. The other intents I'm receiving them just fine.
Thanks,
-Jona
It's probably a broadcast intent that cannot be received using the Manifest. You'll have to setup a BroadcastReceiver object and register it to receive the broadcast. The Receiver must be active in order to receive the intent.
Some broadcasts intents work like this while others are allowed to be caught via the manifest.
The other possibility is that the receiver requires a permission that you aren't requesting.
This is an important note from the SDK about Context.registerReceiver().
Note: this method cannot be called from a BroadcastReceiver component; that is, from a BroadcastReceiver that is declared in an application's manifest. It is okay, however, to call this method from another BroadcastReceiver that has itself been registered at run time with registerReceiver(BroadcastReceiver, IntentFilter), since the lifetime of such a registered BroadcastReceiver is tied to the object that registered it
<uses-permission android:name="android.permission.BROADCAST_STICKY"