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

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.

Related

Android BroadcastReciever - Concept

I tried to wrap my head around Android BroadcastReceiver but with no success. I'm trying to implement something simple:
Listen to an incoming SMS
Check if the number is saved
If it is saved do something
So far I registered / created the BroadcastReceiver and I'm able to catch the incoming messages. I did this in the following way:
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
//do things
}
}
}
I registered the receiver in the Manifest file in the following way:
<receiver
android:name=".SmsReceiver"
android:permission="android.permission.BROADCAST_SMS"
android:exported="true">
<intent-filter android:priority="2147483647" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Everything fine until now.
But, and here is the big BUT: I also created a class called 'UserManager' with a few basic tasks, for example: adding a new user, deleting a user, checking if the user exists, etc.
I store the users in a HashMap (phoneNumber, Name).
My questions are:
How can I pass an Object to my BroadcastReceiver? ( I want to be able to access the HashMap from the "UserManager" class)
I found a lot of topics regarding BroadcastReceivers. Some of them said that there are a couple ways of declaring a broadcast receiver. For example you could do it the way I did ( declaring it in Manifest), or you could do something more uhm... context based? Like declaring it BroadcastReceiver br = new MyBroadcastReceiver() and registering intentFilters to it. What is the difference? Which one should I use?
Is there something called "good practice"? What should I pay attention to? Do you know any material which explains clearly the different ways of using a broadcastReceiver?
You can use dependency injection. Or just hold reference to a class in you Application, that can be accessed by getApplicationContext
Difference is - for receivers declared in manifest you can receiver messages even if you app is in destroyed state. If you broadcast receiver is created manually, well, you must create it before message will be delivered. Also Android forces some restriction on receivers declared in manifest.

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

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.

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

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.

Broadcast receiver for Silent Mode detection? [duplicate]

I'm using a service. In that service my code should get executed when the user changes to silent mode, i.e. as soon as the user changes to silent mode, my code needs to get executed.
How can I do this?
You don't want use a service. Instead you want to use a BroadcastReciever that filters for the android.media.RINGER_MODE_CHANGED Intent.
You might want to take a look at this project as it deals with the phone being silenced. It probably has some source code that will be useful to you.
You can register to listen to the BroadcastAudioManager.RINGER_MODE_CHANGED_ACTION.
In your manifest file you can register the intent like this
<intent-filter>
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
And then recieve the intent at method
public void onReceive(Context context, Intent intent)
of class that extends BroadcastReceiver class

Categories

Resources