I am trying to capture the SMS received on the phone, but when the phone receives an SMS message the method 'onReceive' is not called. This is my code:
I have the BroadcastReceiver is declared in the 'AndroidManifest.xml' inside the tag 'application':
<receiver android:name=".util.IncomingSmsReceiver"
android:exported="true">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
This is the IncomingSmsReceiver.java
public class IncomingSmsReceiver extends BroadcastReceiver {
public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive executed");
if (intent.getAction().equals(SMS_RECEIVED)) {
...
}
}
}
I'm doing the tests on an emulator Google Nexus 5 with Android 6. When I send a sms (fake) in the emulator a notification appears as if it was received really well and I can use it in the default application that brings the emulator. In the logcat of Android Studio does not appear that you have run the method onReceive, or the code written inside.I've tried to change the priority, I've tried using android:enabled="true", I've tried using registerReceiver and I have not gotten it to work. Does anyone know if I miss something?
Are you using a default messaging app with "Disable other apps" flag on?
Please see this:
"android.provider.Telephony.SMS_RECEIVED" not working on my device (HTC Wildfire) - how to debug?
Edit:
Since you are using Android 6, you should use the new permissions model. Check this out:
http://developer.android.com/training/permissions/requesting.html
Related
I've an app that starts itself if the phone is booted. A user told me his phone is used by two people, one of them is using my app and one not.
So I need some event to listen to when the user is switched, so that I can start my apps service if the correct user is using the phone. Anything I can use for that?
Edit
I'm listening to the boot event with a broadcast receiver registered in the manifest, so I know what this is. But I could not find anything suitable for switching users on a device
You need to look for something called BroadcastReciever in android. They are used to capture events such as camera click, phone booting up, screen unlocked etc... These events have a callback called onReceive where you can implement your login.
It's quite easy and you can Google it.
In your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
In your application element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
In MyBroadcastReceiver.java:
package com.example;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
I'm going to make a service which running after device boot completed.
So I added android.permission.RECEIVE_BOOT_COMPLETED permission and a receiver like this:
<receiver android:name=".myapp.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Finally, I create a BootReceiver class extends from BroadcastReceiver like this:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("RECEIVER", "BOOT RECEIVED:" + intent.getAction());
}
}
But it not working. When I reboot my phone, I see a exception from logcat like this:
E/BootReceiver: Can't remove old update packages
java.lang.IllegalArgumentException: Unknown URL content://downloads/my_downloads
at android.content.ContentResolver.delete(ContentResolver.java:1329)
at android.provider.Downloads.removeAllDownloadsByPackage(Downloads.java:1089)
at com.android.server.BootReceiver.removeOldUpdatePackages(BootReceiver.java:93)
at com.android.server.BootReceiver.access$100(BootReceiver.java:42)
at com.android.server.BootReceiver$1.run(BootReceiver.java:82)
When I uninstall my app, this exception still appears.
What's the problem? How can I fix it?
Thanks in advance.
It resolved.
Install app to private memory then it works fine.
Of course, when install an app, I know it install on private memory in standard.
But when you want to get some system receivers, you need to code it.
I am trying to show a toast message when receiving an incoming call/outgoing call.
The receiver is not working if the app is closed.
I do not want to use Service. Please help me out.
'I am using the below receiver code'
public class CallReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
if (isConnected(context)) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Call in progress", Toast.LENGTH_LONG).show();
}
}
}
'This is receiver registered in manifest'
<receiver android:name="com.example.android.testapplication.CallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
<action android:name="android.intent.action.new_outgoing_call"></action>
</intent-filter>
</receiver>
Try adding the phone state permission to your manifest.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
By default when you register a BroadCastReceiver with AndroidOS that means Receiver always work as the Service part even your application is not working, since you do not have to worry about this problem.
I think the problem is the way you register you Receiver was not correct.
With in/out coming call you should use PhoneStateListener which has overrided method onCallStateChanged. You can use 3 states over there.
Maybe this example will be helpful.
I want to receive a sms in my app, but I don't want my Android to show a notification about that event.
i was declare
<receiver android:name="mypackage.SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
written code
public class SMSReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
Object[] pdus = (Object[])extras.get("pdus");
for (Object pdu: pdus)
{
SmsMessage msg = SmsMessage.createFromPdu((byte[])pdu);
String origin = msg.getOriginatingAddress();
String body = msg.getMessageBody();
// Parse the SMS body
if (isMySpecialSMS)
{
// Stop it being passed to the main Messaging inbox
abortBroadcast();
}
}
}
}
and try set priority
<intent-filter android:priority="100">
But not work!Phone display notification!
have Android 4.4.2 (API 19) (Samsung Note 3)
Starting with KitKat (4.4), your app will need to be the default SMS app in order to suppress Notifications, as the default app is responsible for issuing them. Attempting to abort the SMS_RECEIVED broadcast no longer works. Any attempt to abort that broadcast is ignored by the system. Furthermore, the default app listens for the SMS_DELIVER broadcast, which cannot be aborted either, as it is delivered to only the default app. You can consult the following link for information on making your app eligible to be a default SMS app.
Getting Your SMS Apps Ready for KitKat
I am working with Android.
I have an app I am working on uses an Activity to setup specific user input values that are then used by a service to provide alerts based on those values. Doing the research I determined how I could get the app to start up when the phone boots, however, what I really want is to have the service start but not have the app load to the screen. Currently the entire app loads to the screen when I turn on the device and then I have to exit out of it.
I have downloaded similar programs that have interfaces for settings but otherwise run in the background. How is that done?
First you have to create a receiver:
public class BootCompletedReceiver extends BroadcastReceiver {
final static String TAG = "BootCompletedReceiver";
#Override
public void onReceive(Context context, Intent arg1) {
Log.w(TAG, "starting service...");
context.startService(new Intent(context, YourService.class));
}
}
Then add permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and register intent receiver:
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
After this is done, your application (Application class) will run along with services, but no Activities.
Ah, and don't put your application on SD card (APP2SD or something like that), because it has to reside in the main memory to be available right after the boot is completed.