A way to have my BroadcastReceiver block broadcast from system? - java

I am writing an Android app where the user can take pictures and I am using my own camera functionality instead of Androids built in camera software. It all works fine except I want to be able to take a picture when the user presses the hard camera button. I registered a Broadcast receiver, and it works but Android still opens its camera program over my app. Is there a way to block the built in app from receiving the broadcast?
I am posting my code below.
Any help would be greatly appreciated.
Thankyou
//Listen for camera button to be pressed
cameraButtonListener = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_CAMERA_BUTTON)){
Toast.makeText(getApplicationContext(), "Camera Button Pressed", Toast.LENGTH_SHORT).show();
}
}
};
//register broadcast receiver to listen for camera button
getApplicationContext().registerReceiver(cameraButtonListener,new IntentFilter(Intent.ACTION_CAMERA_BUTTON) );

You can use abortBroadcast() in conjunction with android:priority set to high to "consume" the broadcast. However, this works only if the broadcast is an Ordered broadcast, and I don't know what type is ACTION_CAMERA_BUTTON. More info here.

try this code
if("android.intent.action.ACTION_CAMERA_BUTTON".equals(intent.getAction()))
in register
getApplicationContext().registerReceiver(cameraButtonListener,new IntentFilter(Intent.ACTION_CAMERA_BUTTON) );

Related

Audio output broadcasting and Force Routing - Android

I'm trying to build a broadcast that every time audio output route is changed it would route it to a device of choice instead (both in calls and when playing media/music).
for example: when getting a call with plugged/Bluetooth headset and then the user decides to click on the speaker icon on the phone, I need the app to route it back to the headset.
I succeeded to change the audio output on a WIRED headset with a click of a button with following code:
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.setSpeakerphoneOn(false);
and on a Bluetooth headset with the following (not in calls, works on media/music only):
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);
mAudioManager.setSpeakerphoneOn(false);
I have two problems:
When I click on the speaker icon in the call screen to stop using my Bluetooth headset and then click the button on my app to redirct the route to my Bluetooth headset with the above code it doesn't work :(
Didn't manage to find a way to broadcast audio output change in order for the above code to work automatically without a need to press my app's button. only came across the following code that only suppose to work on a Bluetooth headset and not plugged headset and it's don't even work for me on the Bluetooth headset.
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {
int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, -1);
switch (state) {
case BluetoothHeadset.STATE_AUDIO_CONNECTED:
Toast.makeText(context, "Bluetooth Connected", Toast.LENGTH_SHORT).show();
case BluetoothHeadset.STATE_AUDIO_DISCONNECTED:
Toast.makeText(context, "Bluetooth Disconnected", Toast.LENGTH_SHORT).show();
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);
mAudioManager.setSpeakerphoneOn(false);
break;
}
}
}
};
Thanks so much in advance for the help!
any answer would help.

Query regarding Android push notifications

I have an Android app with a working push notification set up. It works currently like this:
User gets a push notification
When the user taps on the push notification, he is taken to a screen which has the list of all the notifications. It is basically a list view which has all the notifications.
Now, want to implement the following:
a. When a new notification comes to the user, it has to be displayed on the list view, irrespective of whether a user taps on it.
b. I have to indicate the number of notifications that user has received on the app icon. For instance, if you get a message in whats app, it displays the number of messages on the app icon.
Could anybody let me know what is the best possible way to implement these functionality? If anybody could point me to tutorials/references, it would be very helpful.
Thanks!
a. Launch an activity from the receiver(GCM/C2DM receiver) when the push notification arrives
// Your C2DM receiver (for GCM check the Android Documentation)
public void onReceive(final Context context, final Intent intent)
{
if (intent.getAction().equals(com.google.android.c2dm.intent.RECEIVE))
{
//start your list view activity of notifications
Intent i = new Intent();
i.setClassName("com.test", "com.test.NotificationsActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
b. Check this.

App won't close after finish() [duplicate]

This question already has answers here:
How to exit from the application and show the home screen?
(22 answers)
Closed 5 months ago.
So, I'm trying to create an app which have a function like an alarm clock for Android phones, it will alert the user at their chosen time. When the alert screen show up, the user has an option to push a notification or snooze, and the app will push a notification and then close.
However, when I click the snooze or the notification button, the app did not close. Instead, the app stop the alarm sound and minimized (just as when the user touch the home button). If I touch the recent app, I then can open the alert screen again.
Edit: I know that there're some codes such as System.exit() or killProcess, since those code are not recommended, I prefer avoid using them.
The reason I ask is because I tested the real clock app that come with my phone (4.3), and the it's alert screen will close after the I press the snooze or dismiss button. So there must be a way for me to do the same, right ?
Answer
Okay, so as Sagar Pilkhwal explained below, and after reading others related problems, I found out that there's no "good" method to close you app by codes, you have to leave that option to the users or OS. Unless you want to use System.exit or killProcess, but they're bad ways to force your app to close.
Sagar Pilkhwal also have a alternatively method to this problem, if you don't want your alert screen show up in recent app, you bring up another activity.
However, as for this case and Alarm Manager in general, open MainActivity will lead to nasty stuffs, since MainActivity is when we handle Alarm events. Then I found out another solution, if you don't want your alert screen show up in recent app, simply add
android:excludeFromRecents="true"
to your Alert.class Activity in the Manifest, or add Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTSto the intent used to start Alert.class.
This will lead to other problem, as when the user press the Home button, the alarm won't turn off, and the user can't open recent app to access the alert screen (he'll has to open the app to do so). I fix this problem by try to detect the home button pressed event. Detail answer are below.
well im thinknig.
System.exit(0);
maybe? im new to android thou..but i sometimes use it..
Put the following to Manifest, in your Alert activity :
android:excludeFromRecents="true" //this will make the Activity be exclude from recents list.
You can also add android:launchMode="singleInstance" and android:taskAffinity="" (if you know what they do)
In the Alert class, use this to detect home button pressed event:
#Override
public void onPause() {
if (!isFinishing()) {
createNotf(); //Handle home button press here.
}
super.onPause();
}
Use this to handle back button pressed: (You have to have this code if you want to detect home button press using isFinishing();
#Override
public void onBackPressed() {
//Handle BackButton event here.
}
For my case only, when the user press home button, I will create a notification, so he can either click on the notification or open the app again to access to Alert Screen.:
private void createNotf() {
Intent screenIntent = new Intent(MyAlert.this, MyAlert.class);
screenIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
screenIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
PendingIntent pIntent = PendingIntent.getActivity(MyAlert.this, MainActivity.SEND_ALARM_CODE, screenIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder ntfBuilder = new Notification.Builder(this)
.setAutoCancel(true)
.setContentTitle("ALARM_RUNNING")
.setLargeIcon(bitmap)
.setSmallIcon(R.drawable.ic_launcher)
.setContentText("CLICK_TO_OPEN_ALERTSCREEN.")
.setContentIntent(pIntent);
NotificationManager myNotfM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotfM.notify(999, ntfBuilder.build());
}
When the Alert stop, that notification will also be clear:
#Override
public void onDestroy() {
super.onDestroy();
NotificationManager mntfM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mntfM.cancel(999);}
System.exit(0);
That right there will close your application out leaving nothing running in the background.However,use this wisely and don't leave files open, database handles open, etc.These things would normally be cleaned up through the finish() command.
I personally HATE when I choose Exit in an application and it doesn't really exit.

populating SMS message in android

Here is the code snippet from our app now.
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sms = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" ));
sms.putExtra("sms_body", text);
startActivity(sms);
}
This works with the stock SMS app, however a tester reported that with handcent SMS they get a blank message. This prompted me to try it with Google Voice, also get a blank message there.
Is there some way I can get my message text to work with all of these other SMS apps?
We have a clipboard functionality, so a poor workaround at least would be having user push clipboard button, then use their messaging app of choice.
Android tries to reuse Intents as much as possible. In some cases when you start an intent, android doesn't create a new Intent and reuse previous intents. To avoid this set this flag of Intent:
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
This flag forces Android to create a new Task with the intent's Activity on top of stack.
When you pass this intent , this will be delivered to all those apps which have registered an activity to receive it. In otherwords you have to use the intent that is registered by the application that u intent to handle your intent

Android API for detecting new media from inbuilt camera & mic

Is there any elegant way in the Android API for detecting new media when it is written to the device? I’m mainly interested in photos taken by the camera, video taken by the camera and audio recorded from the mic.
My current thinking is to periodically scan each media content provider and filter based on last scan time.
I’m just wondering if there is some service I can get realtime notifications.
There's a special broadcast Intent that should get called every time an application writes anything new to the Media Store:
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE
The Broadcast Intent includes the path to the new file, accessible through the Intent.getDataString() method.
To listen for it, just create a BroadcastReceiver and register it using an IntentFilter as shown below:
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String newFileURL = intent.getDataString();
// TODO React to new Media here.
}
}, new IntentFilter(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE));
This will only work for files being inserted into one of the Media Store Content Providers. Also, it depends on the application that's putting it there broadcasting the intent, which all the native (Google) application do.
Aha!
A content observer is what i need!
Here's where i found out about it

Categories