I have been trying to receive broadcast message send by system e.g ACTION_CAMERA_BUTTON. I have been trying to receive the same in our application.
Following is my code
Android Manifest.xml
<receiver android:name=".MyReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_CAMERA_BUTTON" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
</application>
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Uri uri = intent.getData();
Toast.makeText(context,"Got Intent",Toast.LENGTH_LONG).show();
Log.e("Tag",uri.getHost());
}
}
I ran the application in debug mode. When i click the camera button of my device, there is no OnReceive event fired in my app.
What i am missing ?
Do i need to register the broadcast in my main.activity also ?
Add these to your manifest.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
For Camera you need to take runtime permission from user because your Api level 23.
Related
I am writing an application running on my custom device (marshmallow based). the general idea is to prompt a user dialog to enter some information at boot and send it to my server. once the information is successfully sent I update the shared preferences that the information was sent and the dialog will not be prompt anymore.
as always I check to see if the needed permissions are present (read phone state for IMEI and sim info), if not I create an activity that requests permissions and on result starts the user dialog activity. if permissions already exist I create the dialog activity straight from the boot receiver.
however, for some reason, sometimes the permissions activity is created twice (I made sure the boot receiver is only triggered once). this causes as expected the user dialog to be shown twice for no reason. does anyone know how can I solve this? thank you.
here is a code snippet for the boot receiver (the only place where the permissions activity is started).
public class BootReceiver extends BroadcastReceiver {
SharedPreferences mPrefs;
boolean wasUpdated;
#Override
public void onReceive(Context context, Intent intent) {
Log.v("car_num", "got boot receiver");
mPrefs = context.getSharedPreferences("car_num", MODE_PRIVATE);
wasUpdated = mPrefs.getBoolean("is_num_updated", false);
if (!wasUpdated) {
Log.v("car_num", "first time - asking for car num");
if (UtilityFunctions.checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
Log.v("car_num", "we have permission lets ask for car num");
Intent i = new Intent();
i.setClassName("com.mgroup.carnumberapp", "com.mgroup.carnumberapp.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Log.v("car_num", "main activity started");
} else {
Log.v("car_num", "no permission - so requesting");
Intent i = new Intent();
i.setClassName("com.mgroup.carnumberapp", "com.mgroup.carnumberapp.PermissionActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("from", "boot receiver");
context.startActivity(i);
}
} else {
Log.v("car_num", "car number already updated - doing nothing");
}
}
}
edit:
I tried adding
android:launchMode = "singleTop"
however, it happened again.
here is my manifest :
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<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/MGServiceTheme">
<service
android:name=".SendDetailsService"
android:enabled="true"
android:exported="true"></service>
<activity android:name=".PermissionActivity"
android:launchMode = "singleTop"/>
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action
android:name="android.intent.action.MAIN"
android:theme="#style/Theme.AppCompat.NoActionBar" />
</intent-filter>
</activity>
</application>
In your manifest do android:launchMode="singleTop" for an activity
like
<activity android:name=".MapActivity"
android:launchMode="singleTop"/>
<activity android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This question already has answers here:
Android BroadcastReceiver on startup - keep running when Activity is in Background
(6 answers)
Closed 6 years ago.
In order to catch android.intent.action.BOOT_COMPLETED events, is it enough just to register the BroadcastReceiver in the AndroidManifest.xml? like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myDomain.myApp"
android:installLocation="internalOnly">
<!-- ... stuff ... -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- ... stuff ... -->
<application ...>
<!-- ... stuff ... -->
<receiver android:name=".bgServices.MyBootBroadcastReceiver">
</receiver>
<!-- ... stuff ... -->
</application>
</manifest>
The BroadcastReceiver:
package com.myDomain.myApp.bgServices;
// imports ...
public class MyBootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("my-log", "MyBootBroadcastReceiver.onReceive()");
Toast.makeText(context, "YEAY!", Toast.LENGTH_LONG).show();
}
}
I'm asking because I'm sending:
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.myDomain.myApp/.bgServices.MyBootBroadcastReceiver
But nothing happens.
(and I run the app, not just pushing it to the device)
Add the following IntentFilters to your BroadcastReceiver:
<receiver android:name=".bgServices.MyBootBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Although android.intent.action.BOOT_COMPLETED should be enough, it seems some devices require android.intent.action.QUICKBOOT_POWERON also.
For more information, you should take a look at the developer's guide on Intents and IntentFilters.
No That is not. You did not specified the component which will receive the ACTION android.intent.action.BOOT_COMPLETED. So you need to add intent-filter to your receiver
You should do it like below.
<receiver android:name=".bgServices.MyBootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
I configured Parse API in my app and everything works except push notifications. I tried to send them from the site but they don't arrive in app.
I did everything as written in documentation, but I'm not able to receive notification pushes.
I was able to receive one and when I pressed it app crashed. Now I tried to change something but even with reversing I cannot receive them anymore. How can I resolve my problem?
EDIT: Since some users may be interested, here's the parts of the code I used for push notifications:
Main Class (not MainActivity though):
public class InstantsApplication extends Application {
public void onCreate() {
super.onCreate();
// Hidden
Parse.initialize(this, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
ParseInstallation.getCurrentInstallation().saveInBackground();
ParsePush.subscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
} else {
Log.e("com.parse.push", "failed to subscribe for push", e);
}
}
}
}
AndroidManifest (permissions not here included, trust me I have put them):
<!-- PARSE -->
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--
IMPORTANT: Change "com.parse.tutorials.pushnotifications" to match your app's package name.
-->
<!-- I have hidden package name root for this question -->
<category android:name="com.xxxxxxxx.instants" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.ParsePushBroadcastReceiver" android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
</application>
I met this problem before, that because you use the newest Parse api.
There are just a few changes you need to make.
First, to fix the error directly caused by issuing the push from the Parse backend you need to declare a notification icon for the Parse push in your Manifest.
<meta-data android:name="com.parse.push.notification_icon"
android:resource="#drawable/ic_launcher"/>
use before the closing application-Tag.
Issuing another push from the backend now will give you a push notification as you expect. So far so good. Clicking the push will result in an app crash again. To fix this you need to remove the now deprecated call PushService.setDefaultPushCallback(...) and add your own Receiver class. I did this in the *.util package just as the following:
public class Receiver extends ParsePushBroadcastReceiver {
#Override
public void onPushOpen(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Next, change the default Parse receiver to the one just created: - Go to your Manifest file.
<receiver
android:name="your.package.name.utils.Receiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
I have a class which the code as follows:
public class CallManager extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Log.i("onReceive","here");
}
}
Permissions on the manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />
<uses-permission android:name="android.permission.BOOT_COMPLETED" />
Receiver intent filter:
<receiver android:name="com.ram.tapdetector.CallManager">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
For some reason, nothing is getting logged from the onReceive function?
How can I fix this? Any help would be appreciated, thanks.
You need the following in your AndroidManifest.xml file:
1) In your element:
`<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />`
2) In your element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):
<receiver android:name="com.example.CallManager">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
`
In MyBroadcastReceiver.java:
` package com.example;
public class CallManager extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Log.i("onReceive","here");
}
}`
if you are using HTC mobile-
Along with
`<action android:name="android.intent.action.BOOT_COMPLETED" /> `
also use,
`<action android:name="android.intent.action.QUICKBOOT_POWERON" />`
HTC devices dont seem to catch BOOT_COMPLETED
You have to add the permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
instead of
<uses-permission android:name="android.permission.BOOT_COMPLETED" />
for BOOT_COMPLETED. Checkout the documentation here.
As far as I see everything else should be ok.
p.s.: if you want to receive a broadcast event for incoming calls, use permission
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
and declare your intent filter with
<receiver
android:name="com.ram.tapdetector.CallManager"
android:enabled="true">
<intent-filter
android:priority="1000">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Check this nice tutorial for more.
Have you tried with flags:
android:enabled="true"
android:exported="true"
?
This question is outdated, but I have a suggest anyway.
Be carefull, you have twice times the permission :
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
This could generate some errors when you try to run your application.
I am trying to make my app print something to log when screen is turned on but it doesn't work as I expected.
Here is what I have in my Manifest file
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity ...>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="PhoneBroadcastReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"></action>
</intent-filter>
</receiver>
</application>
and my receiver looks like
public class PhoneBroadcastReceiver extends BroadcastReceiver {
public PhoneBroadcastReceiver()
{
}
#Override
public void onReceive(Context _context, Intent _intent) {
// TODO Auto-generated method stub
String a = _intent.getAction();
MessageHandler.log("Received action: " + a); // just a wrapper for printing to a log
}
}
but it prints nothing to the log. I keep pressing my Android power button and the screen turns on / turns off but my message doesn't appear in the log. What am I missing? It looks the same as in examples I found on the web.
You cannot listen for ACTION_SCREEN_ON broadcasts via a BroadcastReceiver registered in the manifest. You have to register it via registerReceiver() from a running component. There are fairly few broadcasts with this trait (ACTION_SCREEN_OFF, ACTION_BATTERY_CHANGED, and perhaps ACTION_USER_PRESENT).
ACTION_SCREEN_ON won't work if registered via manifest file. You need to register it dynamically.
maybe you forget to register the service on
<application> tag:
<service android:name=".YourService" />