The CAMERA_BUTTON intent cannot be caught by receiver [duplicate] - android

I saw many posts in StackOverflow regarding how to listen to camera events, and got few information but still there are few questions remain in my mind please let me know the answers for these:
I have an application which have a broadcast receiver and my broadcast receiver will lauch my activity, but the main purpose of having broadcast receiver is to listen to camera photo/video capture intent.
I want to know which is the intent i have to listen for this, and is it possible to do in this way.
thanks

For Receiving camera photo capture intent, try following code
public class CameraEventReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "New Photo Clicked", Toast.LENGTH_LONG).show();
}
and in manifest, register the receiver:-
<uses-permission android:name="android.permission.CAMERA" />
<receiver
android:name="com.android.application.CameraEventReciver"
android:enabled="true" >
<intent-filter>
<action android:name="com.android.camera.NEW_PICTURE" />
<data android:mimeType="image/*" />
</intent-filter>
</receiver>

In your Android Manifest, you need to specify which intents you want to receive. For camera that'd be the following code (this goes within the <application> tags):
<receiver android:name="com.receiver.CameraReceiver">
<intent-filter android:priority="10000">
<action android:name="android.intent.action.CAMERA_BUTTON" />
</intent-filter>
</receiver>
In addition to that, you should add this to your <intent-filter> within the <activity> tags:
<category android:name="android.intent.category.DEFAULT" />
Finally, take care of the event in your activity's code like so:
#Override
public void onReceive(Context context, Intent intent) {
abortBroadcast();
//TODO: your code here
}

You can use thread that will control your directory camera like:
FileObserver observer =new FileObserver("/mnt/extSd/DCIM/Camera/"){
#Override
public void onEvent(int event, String file) {
// TODO Auto-generated method stub
if(event == FileObserver.CREATE ){
//Do Some things With The file
}
}};
} catch (FileNotFoundException e) {
e.printStackTrace();
}
observer.startWatching();

Related

BroadcastReceiver doesn't work accurately unless registered Manually

To put it simply; BroadcastReceiver Works when defined in Manifest but it works with a short Delay and it doesn't Always trigger unless Registered Manually.
Here is the BroadcastReceiver I created to capture event when date changes (day is passed):
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();
switch (intent.getAction()){
case Intent.ACTION_TIME_TICK:
case Intent.ACTION_TIME_CHANGED:
case Intent.ACTION_TIMEZONE_CHANGED:
case Intent.ACTION_DATE_CHANGED:
case Intent.ACTION_BOOT_COMPLETED:
Log.d("BroadcastReceiver", intent.getAction().toString());
}
}
}
Here registered in Manifest, It works but has a short delay and also doesn't always trigger:
<receiver android:name=".Receivers.BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_TICK" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.DATE_CHANGED"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
But when registered manually via my background service it works just fine:
public class ApplicationService extends Service{
...
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_DATE_CHANGED);
intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
intentFilter.addAction(Intent.ACTION_TIME_TICK);
registerReceiver(new BootBroadcastReceiver(), intentFilter);
return START_STICKY;
}
}
To investigate this issue further I created two similar app which in one BroadcastReceiver registered in Manifest and in the other one receiver is registered Manually via Service.
In the Manifest one all events are triggered few second after Manual one..
Try this :
<receiver android:name=".Receivers.BootBroadcastReceiver">
<intent-filter
android:enabled="true"
android:exported="true">
<action android:name="android.intent.action.TIME_TICK" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.DATE_CHANGED"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
I guess you can have multiple each one having its action.
https://developer.android.com/guide/components/intents-filters.html
The documentation says that "An app component should declare separate filters for each unique job it can do." This would allow finer grained detail for different actions
The answer was hidden in Intent documentation:
ACTION_TIME_TICK Added in API level 1
public static final String ACTION_TIME_TICK
Broadcast Action: The current time has changed. Sent every minute. You
cannot receive this through components declared in manifests, only by
explicitly registering for it with
Context#registerReceiver(BroadcastReceiver, IntentFilter).
To simply put, you can't receive TIME_TICK through registering the BroadcastReceiver in manifest, it has to be registered in your application components (Service, Activity, etc.)
Define a BootBroadcastReceiver anywhere in Activity/Fragment like this:
mBootBroadcastReceiver = new BootBroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG," BootBroadcastReceiver"); //do something with intent
}
};
mIntentFilter=new IntentFilter("action_name");
Now register the BootBroadcastReceiver in onResume() and Unregister in onPause()
#Override
protected void onResume() {
super.onResume();
registerReceiver(mBootBroadcastReceiver, mIntentFilter);
}
#Override
protected void onPause() {
if(mReceiver != null) {
unregisterReceiver(mBootBroadcastReceiver);
mBootBroadcastReceiver = null;
}
super.onPause();
}
add permission in your Manifest.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="ANDROID.PERMISSION.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

Android: How to detect if a broadcaster application is deleted?

I have 2 applications. One of them is doing broadcast custom strings continously and the other one is receiving. I have to be notified and delete some datas in the reciever application when the broadcaster application is deleted. Is there a method like onDelete() or something like that? How can I do this?
Yeah! There's an intent called ACTION_PACKAGE_REMOVED that you can listen for.
Add this inside <application> in your manifest: (don't forget to change the package name)
<receiver android:name="com.arjnklc.receiverapp.UninstallReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
Then you need to create the class mentioned above.
public class UninstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getData().getSchemeSpecificPart() == "com.arjnklc.broadcasterapp")
cleanUpEverything();
}
}
Not exactly sure when you want to do but from what I understand, you want your second application to know when the first application is deleted?
If that's the case, do this:
In AndroidManifest.xml, you MUST have a new BroadcastReceiver because this receiver used a different data scheme:
<receiver
android:name=".PackageReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Then your BroadcastReceiver:
public class PackageReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
Log.d(TAG, "ACTION_PACKAGE_REMOVED");
String data = intent.getData().toString();
// data string has the package name
// if that is your package name, your first app was uninstalled
}
}
}
Just make sure, it's a separate BroadcastReceiver. It can not be combined with any other Receiver or the other actions will stop working.
Hope this works.

Incoming call receiver is not working

I am developing an android application to detect missed calls and I have attached all of my codings below
AndroidManifest.xml :
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<receiver android:name="com.mypackage.MyReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter></receiver>
MyReceiver.java :
public class MyReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent)
{
String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Toast.makeText(context,"Incoming call", Toast.LENGTH_SHORT).show();
if(state==null)
{
Toast.makeText(context,"Null", Toast.LENGTH_SHORT).show();
return;
}
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
Toast.makeText(context,"Ringing", Toast.LENGTH_SHORT).show();
}
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
Toast.makeText(context,"Call received", Toast.LENGTH_SHORT).show();
}
if(state.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
Toast.makeText(context,"Idle", Toast.LENGTH_SHORT).show();
}
}}
Additionally I have two activities in my project (One Main Activity). I think the code is perfect and clean. But it is not working. My Receiver is not detecting any incoming calls since Toast is not displayed while getting a call.That means onReceive() method is not called at all while receiving an incoming call.
I have already tried various solutions suggested in Stackoverflow to the same question asked before long.. But nothing is solved my problem..
If someone suggest a solution, it wil be more helpful...
There is a typo in your intent filter closing tag.
You have used </intent-filter/> where as it should be </intent-filter>. Their is an extra / for closing the tag. Changed code -
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<receiver android:name="com.mypackage.MyReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>

To block the out going call from my android app.

I want to make an application in which once the application starts, it will show two button(start and stop button) and once the user clicks the start button the call function will be blocked for the time period till the user again start the application and click the stop button to stop this function. any help please its urgent
in short I Will tell I want to block the outgoing call from my phone by using this activity only
please is there any way to do so???
You can block the outgoing call using the setResultData(null) function in the onReceive method of the Broaadcast receiver.
public class BlockOutgoing extends BroadcastReceiver
{
String number;
#Override
public void onReceive(Context context, Intent intent)
{
Log.d("12280", "asdasNumber is-->> " + number);
number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
setResultData(null);
Toast.makeText(context, "Outgoing Call Blocked" , 5000).show();
}
}
In the manifest file, you need to register the receiver like this,
<receiver
android:name=".BlockOutgoing"
android:label="#string/app_name" >
<intent-filter android:priority="1">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Also define the permission to intercept the outgoing call,
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Edit-
To unregister a broadcast receiver, follow this link
public class BlockOutgoing extends BroadcastReceiver {
String number;
#SuppressLint("WrongConstant")
#Override
public void onReceive(Context context, Intent intent)
{
// Log.d("12280", "asdasNumber is-->> " + number);
number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
setResultData(null);
Toast.makeText(context, "Outgoing Call Blocked" , 5000).show();
}
}
<receiver
android:name=".BlockOutgoing"
android:label="#string/app_name" >
<intent-filter android:priority="1">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>

method onReceive() of BroadcastReceiver never gets called

I have an app in which I'm trying to register a BroadcastReceiver that listens for intent of this type: android.intent.action.CAMERA_BUTTON but the problem is that my onReceive() method never gets called!
This is how I did:
in onCreate() I've also tried to register this in onResume() but with the same result:
drb=new Adisor();
IntentFilter intent=new IntentFilter("android.intent.action.CAMERA_BUTTON");
registerReceiver(drb,intent);
and my class Adisor:
public class Adisor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("Bau");
if (intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) != null) {
// prevent the camera app from opening
abortBroadcast();
System.out.println("HEY");
// mCamera.takePicture(null, mPictureCallback, mPictureCallback);
}
}
}
And I have the following permissions in the manifest file:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera" />
But when I press the camera button no message is displayed in the logcat!Any idea why?
EDIT: I also tried registering my intent in manifest file
<activity android:name=".TakePhoto"
>
<receiver android:name="com.Contest.Adisor"
android:enabled="true" android:exported="true">
<intent-filter android:priority="10000">
<action android:name="android.intent.action.CAMERA_BUTTON" />
</intent-filter>
</receiver>
Adisor is an inner class of `TakePhoto`.
Try like this.
IntentFilter intentFilter =
new IntentFilter(Intent.ACTION_CAMERA_BUTTON);
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
registerReceiver(drb, intentFilter);
Edited code.
Replace the following code portion.
public class Adisor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("Bau");
if (intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) != null) {
// prevent the camera app from opening
abortBroadcast();
System.out.println("HEY");
// mCamera.takePicture(null, mPictureCallback, mPictureCallback);
}
}
}
with this.
private final BroadcastReceiver drb = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("Bau");
if (intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) != null) {
// prevent the camera app from opening
abortBroadcast();
System.out.println("HEY");
// mCamera.takePicture(null, mPictureCallback, mPictureCallback);
}
}
};
Are you pressing hardware camera button or software button? It is called only when the hardware camera button is pressed, not with the button in the camera application.
EDIT
Also, just found this:
android.intent.action.CAMERA_BUTTON not broadcasting on Desire Z (Froyo)?
There is no requirement for a device manufacturer to send any
broadcast when the CAMERA button is clicked, from my reading of the
Compatibility Definition Document. It might only be used by the
foreground activity on the Desire Z. I don't have a Z and so cannot
confirm your tests.
Since the vast majority of Android devices do not have a CAMERA button
at all, you will need to ensure that your app works well without such
a button, and that you advise users that the CAMERA button may or may
not work with your app depending upon device.
You have to change following changes in your manifest
<activity android:name=".TakePhoto">
<receiver android:name="com.Contest.TakePhoto$Adisor"
android:enabled="true" android:exported="true">
<intent-filter android:priority="10000">
<action android:name="android.intent.action.CAMERA_BUTTON" />
</intent-filter>
</receiver>
Because you had declare broadcast receiver inside your activity TakePhoto

Categories

Resources