Stop BroadCastReceiver from triggering on Application update in Android - android

I have to show prompt when a new Application is installed or uninstalled in the device,so far its working fine. The only problem is prompt is coming even when Application is updated.
How to stop BroadCastReceiver from triggering on Application update.
<receiver android:name=".WeepingReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
BroadCast
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_INSTALL)
|| intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
context.startActivity(new Intent(context, NewAppActivity.class).
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra(Utility.NEW_PACKAGE_NAME, packageName));
}

Try this
Bundle extras = intent.getExtras();
if (extras.containsKey(Intent.EXTRA_REPLACING) && extras.getBoolean(Intent.EXTRA_REPLACING))
{
//do nothing here it is condition of updating your existing app
}else
{
//do your code here
}

Application Update is re-installing a new app so it is correct that your receiver receives the event as PACKAGE_ADDED. Thus, you cannot stop your broadcast from receiving the event.
However, you can validate if the intent is being updated by checking whether the package name was there before. You can have a list of installed apps' package names and store. Then check as you are doing:
if (intent.getAction().equals(Intent.ACTION_PACKAGE_INSTALL)
|| intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)
|| !packageList.contains(packageName))
You could get your package list by:
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
packageList.add(packageInfo.packageName);
}

Just try to change your if condition as
if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED) &&
(intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED))) {
context.startActivity(new Intent(context, NewAppActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(Utility.NEW_PACKAGE_NAME, packageName));
}
And manifest
<receiver android:name=".WeepingReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>

Related

Relaunch the app after phone restarts in flutter

I want to restart my flutter application to automatically relaunch after phone is restarted.
I have done this in android native but now I want the same functionality in my flutter app and tried this method in flutter but its not working in there.Any help would be appreciated
This is my android native code for relaunch of application after mobile restart.
In Manifest
<receiver
android:name=".BroadcastReceiver.RebootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
RebootReciever Class :
public class RebootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) ||
Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())
) {
Intent mainIntent = new Intent(context, com.expo.exposports.ExpoMainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainIntent);
}
}

Unable to fetch referral code after install android app from app store

In my application have a referral program.I want when a customer share app link, that link redirect to playstore and after install app referral code retrieve from playstore. I already do that link but I don't understand how can i receive that referral code after install.
public static void shareMyApp(Context context) {
String link = "https://play.google.com/store/apps/details?id="+context.getPackageName();
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/*");
sharingIntent.putExtra(Intent.EXTRA_TEXT, link);
context.startActivity(Intent.createChooser(sharingIntent, "Share Via"));
}
For that, you have to use below the library
implementation 'com.android.installreferrer:installreferrer:1.0'
https://developer.android.com/google/play/installreferrer/library
https://developer.android.com/training/app-links/deep-linking.html
Thanks, it may help you
Create a link with referral code and send to others friends and users...
String link = http://yourdomain.com/testrefer.php?refercode=ABCD // http or https
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT, link);
context.startActivity(Intent.createChooser(sharingIntent, "Share Via"));
and in testrefer file to create app store link and append refercode and then redirect to play store using below link and then you can get referral code.
https://play.google.com/store/apps/details?id="+context.getPackageName()+"&referrer=ABCD
in Manifest file in your launching screen to write below code
<activity
android:name=".activity.SplActivity"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="yourdomain.com"
android:pathPrefix="/testrefer.php"
android:scheme="https" /> <!-- https or http -->
</intent-filter>
</activity>
InstallRefererReciever is a file name that you have created BroadcastReceiver
<receiver
android:name=".common.InstallRefererReciever"
android:exported="true"
android:permission="android.permission.INSTALL_PACKAGES">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
Create BroascatReceiver and write below code :
public class InstallRefererReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
Bundle extras = intent.getExtras();
if (extras != null) {
String referral = extras.getString("referrer");
Logger.e("ReferCode --> ", referral);
if (referral != null) {
if (!referral.equals("utm_source=google-play&utm_medium=organic")) {
if (!referral.equals("com.android.chrome")) {
String referralCode = referral; // Store in sharedpreferences
}
}
}
}
}
}
}

Open Activity With Dial Code

I want to develop app that doesn't have icon launcher. The app will run alarmscheduler which triggered when app is installed or phone is rebooted.
The problem is how can I open the activity since the app doesn't have intent filter like below:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Is there a way to handle dial code such as ##4635*#*# to open the activity ?
or any other solutions are welcomed.
You can do it with two ways:
1) as answer by #KishuDroid
2) By define code in manifest
public class MySecretCodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
In manifest file
<receiver android:name=".MySecretCodeReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" android:host="4635" />
</intent-filter>
</receiver>
Note:
In Second method you must have to dial *#*#your_code#*#* and dont need to press call button
But in first method you can customise your prefix or postfix of code. For example *#your_code# or **your_code##. but you need to press call button.
You have to use Broadcast Receiver...
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("OutgoingCallReceiver",phonenumber);
Log.i("OutgoingCallReceiver",bundle.toString());
if(code.equals("#056700") {
intent.setComponent(new ComponentName("com.example", "com.example.yourActivity"));
And Your Android Manifest
<receiver android:name="com.varma.samples.detectcalls.receivers.OutgoingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
Also, include the permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

BroadcastReceiver is not able to receive the Intent

I want to write a BroadcastReceiver to receive the application install action. But it failed, so I test if my receiver is well or not. So custom a intent, it also filed. below is my code. Please help me correct it.
public class MyInstallReceiver extends BroadcastReceiver {
// public MyInstallReceiver() {
// }
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
Log.d("receiver", "Intent Detected");
if (intent.getAction (). equals ("android.intent.action.PACKAGE_ADDED")) {
String packageName = intent.getDataString ();
//System.out.println ("installed:" + packageName + "package name of the program");
Log.d("receiver","installed:" + packageName + "package name of the program");
}
}
}
custom intent
public void installAPK(View v){
startActivity(intent);
Intent intent = new Intent();
intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
sendBroadcast(intent);
Log.d("receiver", "Intent sent");
}
Manifest.xml
<receiver
android:name=".MyInstallReceiver"
android:enabled="true"
android:exported="true" >
<Intent-filter>
<action android:name = "android.intent.action.PACKAGE_ADDED"/>
<action android:name = "android.intent.action.PACKAGE_REMOVED"/>
<action android:name="com.tutorialspoint.CUSTOM_INTENT">
</action>
<Data android:scheme = "package" />
</Intent-filter>
</receiver>
enter code here
I don't know about correct spelling in your manifest, but this code is definitely works very well:
<receiver android:name=".MyInstallReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
Every application install/uninstall will trigger this receiver.
Everythong looks good, expect a typo in your manifest. It should be <intent-filter> and not <Intent-filter>

Get newly installed app from BroadcastReceiver:onReceive

Is there has the way I can get newly installed app from BroadcastReceiver onReceive method.
public class PackageAddedReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Package Installed: ", Toast.LENGTH_LONG).show();
}
}
I have set the filter as:
<receiver android:name=".receiver.PackageAddedReceiver" android:label="Package added Receiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
So what's the best way to get newly installed Package's info?
Thanks.

Categories

Resources