How to avoid/cancel application self run due to BroadcastReceiver? - android

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.

Related

Listen for user changed event (multi user device)

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);
}
}

Receiver stops when force close

Hello I've made an app with a receiver to listen to incoming calls ,
My problem is that when i close (swipe off from list of apps) the receiver doesn't work anymore.
first thing i tried is the receiver itself defined in the android manifest like so:
<receiver
android:name=".demo.CallReceiver"
android:exported="true"
android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
<intent-filter android:priority="999">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
This works only when the app is open or in the background .
I looked online and saw this - https://stackoverflow.com/a/46889335/7079340
so i made a Service of my own like so (in the manifest):
<service android:name=".Service.CallService" android:enabled="true"
android:exported="false"> <intent-filter>
<action android:name="com.package.name.IRemoteConnection" />
</intent-filter>
</service>
and the class :
public class CallService extends Service {
private static BroadcastReceiver m_Receiver;
#Override
public IBinder onBind(Intent arg0)
{
Log.e("SERVICELOG","bind");
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("SERVICELOG","start command");
return START_STICKY;
}
#Override
public void onCreate()
{
Log.e("SERVICELOG","create");
Receiver();
}
#Override
public void onDestroy()
{
Log.e("SERVICELOG","destroy");
try{
unregisterReceiver(m_Receiver);}catch (Exception e){
Log.e("SERVICELOG"," "+e.getMessage());
}
}
private void Receiver()
{
m_Receiver = new CallReceiver();
}
}
Started it in the oncreate of my Splashscreen and it prints the logs and it works !
Any way to make this work without a service I'm afraid of battery issues and so on ? Thanks !
Android has had many changes from Marshmallow and above about apps that implicitly listener for broadcast in their manifest. The reasoning for this is because several apps would register for broadcast and a new process would be spun up for all the registered app's broadcast receiver to run in (very expensive) thus causing battery drain. What made this worse, was that users have no control over this behavior because the broadcast receiver couldn't be unregistered. To fix this, the engineering team behind Android, only allows for a select few broadcast to be implicitly registered. One is the Device Boot broadcast intent. By stopping apps from implicitly registering Broadcast's, apps have to be manually launched by the user to listen for intents they'd like to be notified of. This prevent several unnecessary apps from waking up to attempt to handle intent.
As for your concern, about "battery issues" I would recommend to you to use the preferred pattern of explicitly registering a BroadcastReciever in your Service and just performance tune to get your code to be as performant as possible. Services are definitely not free, but they aren't automatically heavy objects just by having one started and running; plus they do exist for this exact purpose. Just remember to not do unnecessary work in your service and you should be off to the right start.
When you close your app, it goes directly to onDestroy method. In your service code, you implemented this method. So, in your method you did stop your service programmatically. In short, you must remove it.

How to fix BroadcastReceiver.onReceive not receive BOOT_COMPLETED action?

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.

Start Service on boot but not entire Android app

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.

SDcard removal notification, how it is done? Broadcast or Service?

I need to implement the sd card removal notification which is already present in android, I need to know how it is being done?? Any sample code or tutorial would be of much help.
you need to use Broadcast Receiver for SD Card Removed
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//If SD Card is Removed it will Come Here
//Intent service = new Intent(context, WordService.class);
//context.startService(service);
}
}
Add Receiver in your Android Manifest File Like Below Code.
<receiver android:name="MyReceiver " >
<intent-filter>
<action android:name="android.intent.action.MEDIA_EJECT" />
</intent-filter>
</receiver>
The system broadcasts Intents on various events, many of which are about the state changes of the SD card (external media).
So you just need to set up a BroadcastReceiver for the proper Intents. Check out this page for reference. You're looking for the ACTION_MEDIA_* Actions.

Categories

Resources