I have an app update receiver which is running fine in debug apk. but after release and update with Google Play build or already installed release build, it's not getting called.
Receiver entry in manifest file is
<receiver android:name=".receiver.AppUpdateReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
and Receiver class is
public class AppUpdateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getAction();
if (action != null && TextUtils.equals(action, Intent.ACTION_MY_PACKAGE_REPLACED)) {
Toast.makeText(context, "updated", Toast.LENGTH_SHORT).show();
}
} catch (Exception ex) {
CTAnalytics.sendExceptionToServer("AppUpdateReceiver onReceive error:", ex);
}
}
}
Looks about right. I'm not sure why there's a need to specify 'android:priority'?
Try removing android:priority and make your receiver exported (I think it should be so by default, since you're specifying an intent-filter). So something like:
<receiver android:name=".receiver.AppUpdateReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
Related
I'm trying to receive a broadcast when power source connects or disconnects to the device. So I mention the receiver in the manifest file but it doesn't work. But When I declare the broadcast dynamically it works fine.
public class PowerConnectionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "POWER CONNECTION CHANGED", Toast.LENGTH_LONG).show();
}
}
manifest code snippet
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
I searched the web for alot of time and I don't understand why my custom broadcast
isn't working.
<receiver
android:name=".myservice.MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
<action android:name="android.intent.action.BATTERY_CHANGED"/>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
</intent-filter>
</receiver>
I don't it not recieve when I reconnet and disconnect the charger.
I did this for making thing simpale
public class MyReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context,"Battery", Toast.LENGTH_SHORT).show();
Log.i("Recive", "Yes");
}
}
From docs:
ACTION_BATTERY_CHANGED
Broadcast Action: This is a sticky broadcast containing the charging state, level, and other information about the battery. See BatteryManager for documentation on the contents of the Intent.
You cannot receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver(). See ACTION_BATTERY_LOW, ACTION_BATTERY_OKAY, ACTION_POWER_CONNECTED, and ACTION_POWER_DISCONNECTED for distinct battery-related broadcasts that are sent and can be received through manifest receivers
So, you cannot use this BroadcastReceiver decalred in Manifest, only registering explicitly from your context.
Also, your power connection BroadcastReceiver seems correct. Try to separate it into another BroadcastReceiver, maybe action ACTION_BATTERY_CHANGED is interfering with other actions.
This is my declared BroadcastReceiver which I use and it's working in my app.
<receiver android:name=".PowerConnectionBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
PowerConnectionBroadcastReceiver
public class PowerConnectionBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "PowerRcvr";
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
Log.d(TAG, "Device is charging");
} else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
Log.d(TAG, "Device is NOT charging");
} else {
Log.d(TAG, "Unable to check if device is charging or not");
}
}
}
NOTE: This code is working on Android 8 with targetSdkVersion 25 or lower.
In targetSdkVersion 26 or higher most of BroadcastReceivers doesn't work through Manifest due to background limitations. Here are documentation (thanks to Pawel) about that. So your IntentFilters wont work. To keep it working you can download your targetSdkVersion to 25 or lower.
I have the app which must start on device' boot. It works well but the problem is that when the app finishes booting, the screen is already dark. How can I make it not fall asleep?
My Receiver:
<receiver
android:enabled="true"
android:exported="true"
android:name=".view.receivers.BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
public class BootReceiver extends BroadcastReceiver {
private final static String LOG_TAG = BootReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.i(LOG_TAG, "Loading after booting...");
Intent startCalendarActivityIntent = new Intent(context, CalendarActivity.class);
startCalendarActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startCalendarActivityIntent);
} else {
Log.e(LOG_TAG, "Error while restarting after boot.");
}
}
}
On this device is Android 6.0 .
You can either use wakelocks, which need certain permissions, or you can put a flag in your onCreate method, which according to the docs does not require a permission.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
The wakelock documentation gave the example above.
I have to implement the referral linking functionality in my application like mCent application. For that I have done the following lines of code.
My application Manifest file. In the <application >..... </application> , I have done some entries for it.
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
<receiver
android:name=".receivers.InstallReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
And My BrodcastRecevier class is as follow , please check it.
public class InstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String rawReferrer = intent.getStringExtra("referrer");
if (rawReferrer != null) {
trackReferrerAttributes(rawReferrer, context);
}
}
private void trackReferrerAttributes(String rawReferrer, Context context) {
String referrer = "";
try {
referrer = URLDecoder.decode(rawReferrer, "UTF-8");
} catch (UnsupportedEncodingException e) {
return;
}
if (Strings.isNullOrEmpty(referrer)) {
return;
}
Uri uri = Uri.parse('?' + referrer); // appends ? for Uri to pickup query string
String memberCode;
try {
referringMember = uri.getQueryParameter("mcode");
} catch (UnsupportedOperationException e) {
return;
}
SharedPreferences.Editor editor = context.getSharedPreferences(
BuildConfig.PACKAGE_NAME, Context.MODE_PRIVATE).edit();
if (!Strings.isNullOrEmpty(memberCode)) {
editor.putString(Constants.REFERRER_CODE, memberCode);
}
String referralMedium = uri.getQueryParameter("tc");
if (!Strings.isNullOrEmpty(referralMedium)) {
editor.putString("referral_medium", referralMedium);
}
editor.apply();
}
}
But i am not receiving any referral from the above code...
I have created the refferal link like this
https://play.google.com/store/apps/details?id=tv.CaseGaurd&referrer=ravindrakushwaha
Is there is any error in my referral link above OR friends , what am i doing wrong in my BroadcastRecevier class or in Manifest file
From this documentation I found that the action filter is (in manifest):
<!-- Used for install referrer tracking-->
<receiver android:name="YOUR_RECEIVER"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
Also be sure that your Receiver is in that real package ".receivers.InstallReceiver", but package com.example.app.receivers;... is your package really com.example.app?
(I considered you to be using the Google Play Store app)... also, about your downvotes, this is likely to bad wording on your question, or that you are not showing effort about your question, finally, note that this is a "free to use community forum", and that people are random...
Finally, put a breakpoint in the Receiver, send a broadcast (using adb for instance), and test that you are really not getting the broadcast.
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();