Android audioReceiver BroadcastReceiver (Android OS Bug) - java

I tried to create a audioReceiver Broadcast. To allow my user to ONLY listen to music if the HeadSet in plug-in.
Here is the code :
private BroadcastReceiver audioReceiver = new BroadcastReceiver()
{ #Override
public void onReceive(Context context, Intent intent)
{ AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
headsetIsPluggedIn = (intent.getExtras().getInt("state")==1);
if(headsetIsPluggedIn) //plugged
{ Log.d("", "BroadcastReceiver - Unmute sound");
audio.setStreamMute(AudioManager.STREAM_MUSIC, false);
}
else //unplugged
{ Log.d("", "BroadcastReceiver - Mute sound");
audio.setStreamMute(AudioManager.STREAM_MUSIC, true);
// Inform user
Toast.makeText(DuplicatedPlayerActivity.this, "Please plug in your headset to enjoy the sound.", Toast.LENGTH_SHORT).show();
}
}
};
#Override
public void onResume() {
super.onResume();
if (HEADSET_ONLY) { registerReceiver(audioReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); }
Manifest
<receiver android:name="com.juno.brheadset.HeadsetStateReceiver">
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG"/>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
OK, Here is the fun part. If you restart the device, run my app. This function will not work.
If you insert the headset jack in the phone at lest once! quit my app, restart the app. The function will work fine, until the user restart the phone again.
Now, why does this function only works if the user insert the jack in the phone?

"Now, Why does this function only works if the user insert the jack in the phone ?" --> Because you have written your code inside the receiver of action "android.intent.action.HEADSET_PLUG". Your code will only execute when you receive the headset_plug broadcast.

Related

Fail to catch android.intent.action.MEDIA_BUTTON

My app that uses TextToSpeech to read some text is 90% finishes but has been stuck at this part for a few days. All I want is have the play/pause button of my bluetooth headset (Xiaomi's Mi Sports Bluetooth Earphones) to play/pause the TextToSpeech. I think it is the android.intent.action.MEDIA_BUTTON I need to catch, so I added these:
In AndroidManifest.xml:
<receiver android:name=".ButtonReceiver">
<intent-filter
android:priority="10000">
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
Then the class ButtonReceiver.java
public class ButtonReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "debug media button test", Toast.LENGTH_LONG).show();
// will finish the code once I catch this intent
}
}
The debug text does not show up. Though if I changed <action android:name="android.intent.action.MEDIA_BUTTON"/> to <action android:name="android.media.VOLUME_CHANGED_ACTION"/>, it does show the text when I press the volume up or down of the headset but this is not exactly what I want. I only want the app to respond to the play/pause button.
Then I read that I need to use registerMediaButtonEventReceiver, so I tried adding this in my MainActivity.java:
private AudioManager audioManager;
private ComponentName componentName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audioManager = (AudioManager) this.getSystemService(AUDIO_SERVICE);
componentName = new ComponentName(this, ButtonReceiver.class);
audioManager.registerMediaButtonEventReceiver(componentName);
}
Still not working. Also, it says the registerMediaButtonEventReceiver is deprecated, so I am wondering if this is the reason that it does not work.
Further reading from the official document page, it tells this:
If you are running Android 5.0 (API level 21) or later, call
FLAG_HANDLES_MEDIA_BUTTONS
MediaBrowserCompat.ConnectionCallback.onConnected. This will
automatically call your media controller's dispatchMediaButtonEvent(),
which translates the key code to a media session callback.
I find this stupid, as I do not need this extra module to play media. All I need is the detection of the headset button press. Anyway, I tried to implement the MediaSession but gave up soon as it quickly ended up with too many codes that were useless for my app since my app is NOT an audio player app!
Any suggestion? What should I do?
I found the solution myself finally! Instead of making a separate BroadcastReceiver class, I added a public static class in my MainActivity.java:
public static class ButtonReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
Toast.makeText(context, "debug media button test", Toast.LENGTH_SHORT).show();
...
}
}
Then added this line in onCreate:
((AudioManager)getSystemService(AUDIO_SERVICE))
.registerMediaButtonEventReceiver(new ComponentName(this, ButtonReceiver.class));
And finally in AndroidManifest.xml:
<receiver android:name=".MainActivity$ButtonReceiver"
android:enabled="true">
<intent-filter
android:priority="10000">
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
It works! The app can now catch the MEDIA_BUTTON intent with this setting!

why restarting a device that has an application that restricts the use of UserManager.DISALLOW_USB_FILE_TRANSFER renders the device unusable?

I'm have created a kiosk application by following this guide in android developers - Lock Task Mode. The application can start itself automatically whenever the device finish booting up but the problem is whenever I restart/shutdown and start my device that has this restriction the device will not be able to boot it will stuck in the brand logo screen of the device and have to factory reset it to work again.
dpm.addUserRestriction(componentName, UserManager.DISALLOW_USB_FILE_TRANSFER);
This restriction is working fine if I don't restart the device but at some point the device needs to be shutdown. How can I set this restriction properly during start without breaking the deivce?
I found a workaround to solve the problem. I have created a BroadcastReceiver for device shutdown and remove the restriction and re-enable the restriction when the device restarts.
public class ShutDownReceiver extends BroadcastReceiver {
private static final String TAG = "ShutDownReceiver";
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_SHUTDOWN.equals(action)){
DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName cn = AdminReceiver.getComponentName(context);
if (dpm != null && dpm.isDeviceOwnerApp(context.getPackageName())) {
//This is a custom method
setUserRestriction(dpm, cn, UserManager.DISALLOW_USB_FILE_TRANSFER, false);
}
Toast.makeText(context, "Shutting Down", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onReceive: ACTION_SHUTDOWN");
}
}
}
Add code in manifest
<receiver android:name=".receiver.ShutDownReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>

Broadcast receiver not working when app is closed

So I have two different apps made, one sends a broadcast and another receives it and displays a toast. However, when I close the receiver app the broadcast is no longer received by the second app even though I defined the receiver in the manifest file.
The broadcast sender in the MainActivity of app1.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent();
i.setAction("com.example.ali.rrr");
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(i);
Log.e("Broadcast","sent");
}
});
}
App 2 broadcast receiver:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context, "Broadcast has been recieved!", Toast.LENGTH_SHORT).show();
Log.e("SUCCESS", "IN RECIEVER");
//throw new UnsupportedOperationException("Not yet implemented");
}
App 2s Manifest:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.ali.rrr" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Main2Activity"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
After registering my BroadcastReceiver (BR) statically in the manifest, applying the proper intent filters, using JobIntentService (and registering it in the manifest) to handle the work that was called from my BR, I was still getting inconsistent results.
Once all of what I listed above has been done you should be able to send an ADB command to activate your broadcast and process the work in your service even if the app is closed. This was working for me some of the time, but not all of the time.
This article describes limitation to BRs.
"As of Android 3.1 the Android system excludes all receiver from receiving intents by default if the corresponding application has never been started by the user or if the user explicitly stopped the application via the Android menu" (AKA a user executes Force Stop)
When I start the app by debugging it, then swipe it closed on my device, my ADB command never activates my BR. However, after my debugging session is over, when I open up the app on my device and swipe it closed, I can activate my BR through ADB commands.
This occurs because when you debug an application, then manually swipe it closed on the device, Android considers this a Force Stop hence why my BR cannot be activated until I re-open the app on the device without debugging.
Scoured the internet for hours, and wasn't able to find my solution, so I thought I'd post it here just in case some poor unfortunate soul is encountering the same weird functionality I was.
Happy coding :)
First of all you need to use the Service for this functionality to work.
In the Activity you can start and stop the service by using the below codes.
// to start a service
Intent service = new Intent(context, MyBrodcastRecieverService.class);
context.startService(service);
// to Stop service
Intent service = new Intent(context, MyBrodcastRecieverService.class);
context.stopService(service);
Then you can use the below service
public class MyBrodcastRecieverService extends Service
{
private static BroadcastReceiver br_ScreenOffReceiver;
#Override
public IBinder onBind(Intent arg0)
{
return null;
}
#Override
public void onCreate()
{
registerScreenOffReceiver();
}
#Override
public void onDestroy()
{
unregisterReceiver(br__ScreenOffReceiver);
m_ScreenOffReceiver = null;
}
private void registerScreenOffReceiver()
{
br_ScreenOffReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, "ACTION_SCREEN_OFF");
// do something, e.g. send Intent to main app
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(br_ScreenOffReceiver, filter);
}
}
I faced this issue recently. The BroadcastReceiver was working fine even if the app was removed from the background in the emulator and Samsung phones. But it failed to start my app in Chinese manufactured phones like Realme, Mi etc. While struggling to find a way to fix this I found that in the app details page there is battery optimisation settings where the Auto-launch feature was disabled. After I enabled it the app was working fine and BroadcastReceiver was able to start the app. I was unable ti find a way to enable this setting programmatically but I found this question which helped me direct the user to that setting page.
You can go through below solution;
Activity.java
Intent intent=new Intent(MainActivity.this,BroadcastService.class);
startService(intent);
BroadcastService.java
public class BroadcastService extends Service {
private static MusicIntentReceiver br_ScreenOffReceiver;
#Override
public IBinder onBind(Intent arg0)
{
return null;
}
#Override
public void onCreate()
{
registerScreenOffReceiver();
}
#Override
public void onDestroy()
{
}
private void registerScreenOffReceiver()
{
br_ScreenOffReceiver = new MusicIntentReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.e("AAAAAAAAAA", "Headset is unplugged");
break;
case 1:
Log.e("AAAAAAAAA", "Headset is plugged");
break;
default:
Log.e("AAAAAAAAAAAA", "I have no idea what the headset state is");
}
}
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(br_ScreenOffReceiver, filter);
}
}
Menifest
<service android:enabled="true" android:name=".BroadcastService" />
Try this way..
Intent i = new Intent();
i.setAction("com.example.ali.rrr");
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
i.setComponent(
new ComponentName("PackageNameApp2","PackageNameApp2.MainActivity"));
sendBroadcast(i);

Shake Device to Launch App

I am using this to work with Shake, and that works fine for me, but i wanna launch application when user shake their device,
see my code below:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
transcript=(TextView)findViewById(R.id.transcript);
scroll=(ScrollView)findViewById(R.id.scroll);
shaker=new Shaker(this, 1.25d, 500, this);
}
#Override
public void onDestroy() {
super.onDestroy();
shaker.close();
}
public void shakingStarted() {
Log.d("ShakerDemo", "Shaking started!");
transcript.setText(transcript.getText().toString()+"Shaking started\n");
scroll.fullScroll(View.FOCUS_DOWN);
}
public void shakingStopped() {
Log.d("ShakerDemo", "Shaking stopped!");
transcript.setText(transcript.getText().toString()+"Shaking stopped\n");
scroll.fullScroll(View.FOCUS_DOWN);
}
So here is my question, how can i launch an application by shaking my device ?
You should write Android Service that will start your activity during shake. That is all. Services run in the background even if Activity is not visible
Service can be started eg. during device boot. This can be achieved using BroadCastReceiver.
Manifest:
<application ...>
<activity android:name=".ActivityThatShouldBeLaunchedAfterShake" />
<service android:name=".ShakeService" />
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
BootReceiver:
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent intent = new Intent(context, ShakeService.class);
context.startService(intent);
}
}
Service:
public class ShakeService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
... somewhere
if(shaked) {
Intent intent = new Intent(getApplicationContext(), ActivityThatShouldBeLaunchedAfterShake.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
Write a separate app for Shake detection. On detection of shake, fire an intent with the package name of app, you want to launch:
Intent intent = new Intent (<PackageNameOfAppToBeLaunched>);
startActivity (intent);
Well What you need is Two different activity Where first One Detects your Shake which need to run all time in background and than call the new actual app you want to run on shake.
You can run your background activity you have to use class which will keep your activity run in background for long time(Continuously) you can use classes Like FutureTask or Executor (you can not use AsyncTask for this).
Whenever the thread passes command to Your Application open after Shake the Background process stops and command goes to app means you need to again immediately start background process after the actual app closed.
You need to write the code to launch the application to foreground from background while the shake started. This link will help you to do so.

Registering a headset button click with BroadcastReceiver in Android

I have a headset with single button and want to do a simple Toast when the button is pressed.
Right now I have the following code:
public class MediaButtonIntentReceiver extends BroadcastReceiver {
public MediaButtonIntentReceiver() {
super();
}
#Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
return;
}
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
// do something
Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show();
}
abortBroadcast();
}
}
And my main activity is the following:
public class mainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
registerReceiver(r, filter);
}
}
Nothing happens though when I push the button though.
I'm pretty sure something is wrong with my permissions/xml in the manifest. Here's the receiver XML so far:
<receiver android:name=".MediaButtonIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
....
and:
<uses-permission android:name="android.permission.BLUETOOTH" />
I notice in LogCat that when I press the button I get an error from "BluetoothIntentReceiver" saying "onReceive() Action : android.intent.action.MEDIA_BUTTON"
Just wanted to answer my own question in case others come across similar issues.
The code does work, just I wasn't seeing the Toast because I had another headset button controller app installed (and running in the background), so I guess it took priority over mine. However when I put
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);//"android.intent.action.MEDIA_BUTTON"
MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
filter.setPriority(1000); //this line sets receiver priority
registerReceiver(r, filter);
It was able to work even with the other app installed. Also, you don't need both the above AND the XML, one or the other is fine as ways of registering the intent receiver.
Here's what I've got that's working in Android 4.2.2
In my manifest.xml I do this:
<receiver android:name=".MediaButtonIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
NB: this is instead of calling registerReceiver.
In my Main Activity's onCreate I need to call the AudioManager:
((AudioManager)getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(
new ComponentName(
getPackageName(),
MediaButtonIntentReceiever.class.getName()));
I have found it will work without the AudioManager call, but not for long!
You shouldn't use setPriority
You register your broadcast receiver in the manifest
You then register your broadcast receiver using:
AudioManager#registerMediaButtonEventReceiver
The argument to registerMediaButtonEventReceiver is a ComponentName that points your broadcast receiver.
For a fully documented answer for Android >4.0 have a look here:
BroadcastReceiver for ACTION_MEDIA_BUTTON not working
If you don't want to use BroadcastReceiver, you can do this for Android >5.0 (API level 21 LOLLIPOP) using the MediaSession described here: https://stackoverflow.com/a/39413753/1386969

Categories