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.
Related
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
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 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.
I have application with BroadcastReceiver which listens to SD card mount/unmount, like:
public class ExternalDatabaseRemovingBroadcastReceiver extends BroadcastReceiver
{
private static final String TAG= ExternalDatabaseRemovingBroadcastReceiver.class.getName();
public ExternalDatabaseRemovingBroadcastReceiver()
{
super();
}
#Override
public void onReceive(Context context, Intent intent)
{
if(Me.DEBUG)
Log.d(TAG, "SD card mount/unmount broadcast=" + intent.getAction());
if(intent.getAction()==null)
return;
if(Intent.ACTION_MEDIA_UNMOUNTED.equalsIgnoreCase(intent.getAction()) ||
Intent.ACTION_MEDIA_EJECT.equalsIgnoreCase(intent.getAction()) ||
Intent.ACTION_MEDIA_SHARED.equalsIgnoreCase(intent.getAction()))
{
//blah-blah
}
}
}
Broadcast is declared in AndroidManifest as:
<receiver android:enabled="true"
android:exported="true"
android:name=".ExternalDatabaseRemovingBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<action android:name="android.intent.action.MEDIA_SHARED"/>
<data android:scheme="file"/>
</intent-filter>
</receiver>
And now my problem. During device launch (either real or emulator) - my application unintentionally runs. I mean ActivityManager self runs it reporting:
11-22 08:56:52.239: INFO/ActivityManager(61): Start proc ru.ivanovpv.cellbox.android for broadcast ru.ivanovpv.cellbox.android/.ExternalDatabaseRemovingBroadcastReceiver: pid=288 uid=10034 gids={1015}
Please explain what's goin on? And how to avoid application self running?
It does sound like the system is responding to the SD card mounting at boot. You could whitelist access to starting this BroadcastReceiver by removing android:exported="true" or changing it to false, and enabling the use of <permission>. I don't know what your end goal of this is, so that may not be the best course of action.
It seems to me that upon booting the device, the SD card is mounted as well, which triggers your intent filter. If you don't want this 'initial' mount to be registered by your app, you can perhaps ignore mounts that happen during the first x seconds of uptime. That may not be the most elegant solution, though...
Edit: Now that I understand barmaley's original intent, the solution is much simpler. Intent-filters in the Android manifest are meant to start your application when something external happens. If you only want to react to (un)mounts while your application is already running, just register your broadcastreceiver programmatically in Application.create and unregister it in Application.destroy using Context.registerReceiver and Context.unregisterReceiver respectively.
As my code looks today, I'm periodically sending a alarm(?) using AlarmManager that is received by AlarmReceiver extends BroadcastReceiver which in turn starts a Service. The Service do some updating and ends with a stopSelf(). IMO this is the best way of periodically perfom a task without constantly having a Service running. Correct?
The issue with this code is however that the whole chain of events is initiated onSharedPreferenceChanged(). I (initially) thought this was a good idea since the whole updating thing is enabled by the user in SharedPreferences.
I've now come to the conclusion that this is in fact not very good and that I need to initiate the AlarmManager/AlarmReceiver/Service/whatever both onPreferenceChange but also on boot.
I've done some searching but everyone seems to want to start the Service on boot. As I see it, I just need to initiate the AlarmManager which will then start the Service (when needed and only periodically).
Please help me with, first of all, sorting this out and secondly coding it!
Thanks in advance!
Then, create and register a BroadcastReceiver where you will do the AlarmManager stuff:
public class YourBootReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// do the AlarmManager here
}
}
Then, on your manifest:
<application>
... other stuff
<receiver android:name=".YourBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />