Broadcast receiver for Silent Mode detection? [duplicate] - android

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

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

ACTION_SCREEN_OFF background service trigger

I have a notification being fired through AlarmManager and the notification also
plays a sound.
Obviously, it may happen that the alarm is fired when the app is in the background, and I would like to let the user cancel the sound when pressing the lock button - i.e. listening for ACTION_SCREEN_OFF.
Therefore I wonder if it's possible to start a service and listen for ACTION_SCREEN_OFF?
I have seen Listening for ACTION_SCREEN_OFF but that solution of having a BroadCastReceiver only seems to work when the app is in the foreground. Right?
For instance if you are trying to do ACTION_SCREEN_OFF then you would define your broadcast receiver in your Activity that started the alarm for instance.
public class SomeListener extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
//Turn off sounds
}
}
Then in the manifest provide something of the sort like this within the activity that uses the listener.
<intent-filter>
<action android:name="android.hardware.usb.action.ACTION_SCREEN_OFF" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.ACTION_SCREEN_OFF"
android:resource="#xml/my_filter" />
Where the extra my_filter class could provide additional meta-data. In this case it was a check against a Serial or UUID of the device so not to launch on all USB connects. But you should be able to do something similar.
When the action occurs, this should fire the listener within your application, as I understand it anyways. This feature works for launch of application on USB connect for me in the past. Even without having had the application open in the first place.

Execute code when user changes to silent mode in Android

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

Starting Android Service on boot and PreferenceChange

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" />

onBoot Complete as user preference

Is there a way that trough the application, I can subscribe and unsubscribe for the "ACTION_BOOT_COMPLETED" message?
If so, how can I do this?
Any kind of pointer will help me.
Thanks in advance,
Regards,
Vinay
Check out my answer to this question. And these links.
http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/
Trying to start a service on boot on Android
Android: how to start a service at boot based on user-settings?
The best way to do this is use PackageManager.setComponentEnabledSetting(): http://developer.android.com/reference/android/content/pm/PackageManager.html#setComponentEnabledSetting(android.content.ComponentName, int, int)
So simply decide whether you want your receiving to be enabled or not by default by setting android:enabled in the manifest. Then use this API to explicitly enable or disable the component at run time as desired. If the component is disabled, at boot it will not be available, and thus not receive the broadcast.
As a matter of fact, there is:
Your boot receiver:
public class BootstrapReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context cntxt, Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_BOOT_COMPLETED.equals(action)) {
// do your thing
}
}
}
Add a receiver and a permission for receiving boot events:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
....
<receiver android:name=".BootstrapReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Via the application (Use this if you want to programatically add/remove boot receiver and not via AndroidManifest.xml. But remember, you will still have to declare the boot permission in your AndroidManifest.xml)
Context ctx = getContext();
BootstrapReceiver booter = new BootstrapReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
ctx.registerReceiver(booter, filter);
Unregister:
ctx.unregisterReceiver(booter);

Categories

Resources