method onReceive() of BroadcastReceiver never gets called - android

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

Related

how to use broadcast receiver on receive

i have a project and when i run the application,one service should be active and when the device is turned on,my android service should be active.
The project runs in emulator successfully but in my phone when i turn on the device it doesn't works!
my broadcast receiver :
public class BroadcastReceiverOnTurnedOn extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
i added :
<receiver android:name="com.dariran.BroadcastReceiverOnTurnedOn">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
to appliation tag on Manifest.xml and
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
You are registering a BroadcastReceiver not a Service. Services will generally run in the background all of the time (if memory and resources permit).
Your broadcast receiver activates only when the device is rebooted - and your emulator "boots" every time you start it up. Your phone does not "boot" when the app is installed.
You should either register for events like "app installed" or else actually implement a service, like here:
http://www.vogella.com/tutorials/AndroidServices/article.html
You need to make sure that once you install the app on device it should be started once by clicking app icon then only boot receiver will receive boot event on subsequent reboots calls.
i undrestand that if i install app in internal storage in works successfully even device rebooted but when i install the app on external storage and then reboot device it not work
i have a project that when run application one service be active and when device be turn on my android service be active. the project run in device's internal memory successfully but when i install it on external memory and reboot my device again don't work!
for first i call my service in first acitivity and it works, but when i reboot my device it doesn't work !
my activity :
public class FirstClass extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
startService(new Intent(getApplicationContext(), MyService.class));
startActivity(new Intent(FirstClass.this, MainActivity.class));
finish();
}
},5000);
}
/////////////////////////////////////////////////
}
my broadcast receiver :
public class BroadcastReceiverOnTurnedOn extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
i added :
<service
android:name="com.dariran.MyService"
android:enabled="true"
android:exported="true" >
</service>
<receiver android:name="com.dariran.BroadcastReceiverOnTurnedOn"
android:enabled="true">
<intent-filter android:priority="1">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>to appliation tag on Manifest.xml and
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
i added this code to my service class to put a filter to recognize the external storage but don't work again :(
#Override
public void onStart(Intent intent, int startId) {
try {
IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
BroadcastReceiver mReceiver = new BroadcastReceiverOnTurnedOn();
registerReceiver(mReceiver, filter);
} catch (Exception e) {
}
}

How to receive brocast with VOICEMAIL

http://developer.android.com/reference/android/provider/VoicemailContract.html
i don't know how to make a brocast receiver with VOICEMAIL. When you receive a voicemail,you will see "NEW VOICEMAIL" in notification,the icon just like a tape.
here is my code:
AndroidManifest.xml
<receiver android:name=".VoiceBrocast" >
<intent-filter>
<action android:name="android.intent.action.NEW_VOICEMAIL" />
</intent-filter>
</receiver>
<uses-permission android:name="com.android.voicemail.permission.ADD_VOICEMAIL" />
i tried to register in xx.java,but it's no works.
MainActivity.java:
protected VoiceBrocast mUiBroadcastReceiver;
mUiBroadcastReceiver = new VoiceBrocast();
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.intent.action.ACTION_NEW_VOICEMAIL");
MainActivity.this.registerReceiver(mUiBroadcastReceiver, mIntentFilter);
VoiceBrocast.Java
public class VoiceBrocast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("voicemail");
}
}
when i received a voicemail, nothing to print, i can't see "voicemail" in LogCat.
Not sure that else is wrong, but one problem is: the intent action is not called "android.intent.action.ACTION_NEW_VOICEMAIL" but "android.intent.action.NEW_VOICEMAIL".
Perhaps you mixed it up with the constant name on the VoicemailContract class which is VoicemailContract.ACTION_NEW_VOICEMAIL.
The minimum permission needed to access this content provider is ADD_VOICEMAIL
Do you have such permission ?

How can I track an APK install

I'm trying to track an APK install. When a user lands on the downloadpage (not the store), he is coming from a specific source. When user clicks on download, the APK will be installed. After it's installed, I need to map the install to the source the user was coming from before installing. Is there any good way to do this?
My plan so far: Save the user IP and screen resolutions on the download page to a database. After install, pass IP and screen resolution to the server and map with the row in the database. Is this a good way of doing this?
Hope you guys can help me.
You just need to write a BroadcastReceiver for this which can receive the PACKAGE_ADDED and PACKAGE_INSTALL Intent:
InstallBroadcastReceiver.Class
public class InstallBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_PACKAGE_ADDED)
||action.equals(Intent.ACTION_PACKAGE_INSTALL)){
notifyServerForApplicationInstall(context, intent);
}
}
private void notifyServerForApplicationInstall(Context context,Intent intent){
//send the data to your server here
}
}
Register the receiver in AndroidManifest file
<receiver
android:name=".InstallBroadcastReceiver"
android:exported="false"
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Don't forget to give this permissions in manifest :
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
I have prepared a BroadcastReceiver class :
public class newPackageReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("DEBUG"," test for application install/uninstall");
}
}
In the main activity, I first register a new receiver object, then instanciate button for application install.
public void onCreate(Bundle savedInstanceState) {
...
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
filter.addAction(Intent.ACTION_PACKAGE_INSTALL);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
receiver = new newPackageReceiver();
registerReceiver(receiver, filter);
...
dlButton.setText(R.string.dl_button);
dlButton.setOnClickListener(new AppliDownloadOnClickListener(this ));
#Override
public void onDestroy(){
unregisterReceiver(receiver);
super.onDestroy();
}

broadcast receiver for ACTION_CAMERA_BUTTON never gets called

I have an app in android in which I wanna take a photo when physical hardware button for camera gets pressed.I registered an intent for this type of action but my broadcast receiver never gets called.
Here is how I did it:
class that extends BroadcastReceiver
public class Adisor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) != null) {
// prevent the camera app from opening
abortBroadcast();
System.out.println("HEY");
mCamera.takePicture(null, mPictureCallback, mPictureCallback);
}
}
}
Here is where I register my receiver to listen for actions:
protected void onResume() {
Log.e(TAG, "onResume");
super.onResume();
drb = new Adisor();
IntentFilter i = new IntentFilter(
"android.intent.action.CAMERA_BUTTON"
);
registerReceiver(drb, i);
}
And in my manifest file I have this:
<activity android:name=".TakePhoto" />
<receiver android:name=".Adisor" >
<intent-filter android:priority="10000">
<action android:name="android.intent.action.CAMERA_BUTTON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
The name of the activity in which I'm doing all this is:TakePhoto.My question is why my onReceive() method never gets called!
Neither this:
System.out.println("HEY");
appears in my logcat or the method
System.out.println("HEY");
mCamera.takePicture(null, mPictureCallbacmPictureCallback);
gets called!
What I'm doing wrong?
You should either have the receiver registered in the manifest or register programmatically. Remove the registerReceiver() call from the onResume method.
Edit:
Add these to your manifest.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
Your intent filter should never have a priority of 10000. The maximum allowed for user applications is 999.
See setPriority(int) on the AndroidDev website.
For opening the only the camera of your application you can use intent like:
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, ACTION_IMAGE_CAPTURE);

The CAMERA_BUTTON intent cannot be caught by receiver [duplicate]

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

Categories

Resources