How to detect that new app has been installed on the device? - android

How does CleanMaster app detect that a new app has been installed on the device? Whenever I install a new app, I get a popup asking if I want to move the app to SD card.
I am trying to code similar behaviour but cannot find a way to do it.

There is the ACTION_PACKAGE_ADDED Broadcast Intent, but the application being installed doesn't receive this.
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>

Android provides String android.content.Intent.ACTION_PACKAGE_ADDED ="android.intent.action.PACKAGE_ADDED" Broadcast Action: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does not receive this broadcast.
You can write a BroadcastReceiver receiving the Intent.ACTION_PACKAGE_ADDED for that.

For this You need to write a receiver class like this
public class AppInstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
//Perform Your opeartion
}
}
And register it in manifest like.
<receiver android:name="com.example.AppInstallReceiver" >
<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>

Related

android - broadcast handle app package change(uninstall)

i need to handle app package change, i write my mainfest like that
mainfest.xml
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
my receiver class
public class PackageChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("app changed thank you ");
// here i will handle each one as i like
//if(intent.getAction().equalsIgnoreCase("android.intent.action.PACKAGE_REMOVED"))
// do some thing etc
}
}
but i dosnt work , i install , delete broadcast not notified
please help me to fix it
thank you
ok
i compiled your code
its working after adding
<action android:name="android.intent.action.PACKAGE_INSTALL" />
//work for other app uninstalled but dont test by uninstalling this app
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
to your code.
you need permissions for package
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Cannot receive Uninstall broadcast

I am trying to detect the uninstall action of my application. Till now, I have got a specific code that catch the uninstall action and inflate an Activity. Here is the code:
Manifest:
<activity
android:name=".UninstallActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DELETE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</activity>
I have created a simple Activity called UninstallActivity and It works fine. When the user try to uninstall the app this Activity has been inflated.
I am trying to listen on those intents with a Receiver instead of Activity but I have failed to get this action. My code is:
Manifest:
<receiver android:name=".PackageUninstallReceiver" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DELETE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</receiver>
PackageUninstallReceiver:
public class PackageUninstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("uTag", "In the PackageUninstallReceiver onReceive()");
if (intent.getAction().equals(Intent.ACTION_DELETE) && intent.getDataString().contains(context.getPackageName())) {
Log.d("uTag", "Uninstallation is being happened....");
}
}
}
First, is it possible to listen to this Intent with the receiver?
If yes, what is wrong with my code?
The Intent used to start an Activity (in this case, an Intent to VIEW or DELETE a PACKAGE) is a completely different thing from a braodcast Intent. They share some of the same properties, but are still completely different things. a broadcast Intent will never start an Activity and an Intent used to start an Activity will never be seen by a BroadcastReceiver.
Therefore, the answer to your question
First, is it possible to listen to this Intent with the receiver?
is "no".
The actions you are listening are generic and could be applied in any context with a different schema. What you should be listening to is the package changing.
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>

How I can install apk automatically when the sdcard put on?

I have not any idea. How can I install apk automatically when the sdcard put on?
However, I have got an problem, When I register the receiver that listened to the ACTION_MEDIA_SHARED in AndroidManifest.xml Em... I create a Receiver that extends BroadcastReceive, I override OnReceive(). But, finally, the Receiver can not get any Action. Here is my code. Frustrating!!!!!
<receiver android:name=".SdcardPutOnListener"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_MEDIA_BAD_REMOVAL" />
<action android:name="android.intent.action.ACTION_MEDIA_MEDIA_CHECKING" />
<action android:name="android.intent.action.ACTION_MEDIA_EJECT" />
<action android:name="android.intent.action.ACTION_MEDIA_MOUNTED" />
<action android:name="android.intent.action.ACTION_MEDIA_NOFS" />
<action android:name="android.intent.action.ACTION_MEDIA_REMOVED" />
<action android:name="android.intent.action.ACTION_MEDIA_SHARED" />
<action android:name="android.intent.action.ACTION_MEDIA_UNMOUNTABLE" />
<data android:scheme="file" />
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
public class SdcardPutOnListener extends BroadcastReceiver {
final static String TAG = "SdcardPutOnListener";
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "receive broadcast " + intent.getAction());
}
}
You could create an app that watches for the ACTION_MEDIA_MOUNTED broadcast, then looks at external storage in a well-known spot for an APK file, then calls startActivity() with an ACTION_VIEW Intent on the path to that APK file, with the right MIME type (application/vnd.android.package-archive).
If you are expecting this to be built into the operating system, it is not.

Android: receiving intent sent by system ACTION_PACKAGE_RESTARTED

I am new to android. I get completely stuck in using ACTION_PACKAGE_RESTARTED in my application
I have removed pacakge from my emulator, also added using adb install but get nothing. Start an app. close that one and again start that app. nothing seems work for me. There is no log in logcat.
Is there anything that i'm missing? Please help
public class RestartReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action= intent.getAction();
Log.i("D", "Inside receiver");
}
And here is the manifest file
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".ReceiverTest">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.ACTION_PACKAGE_RESTARTED" />
</intent-filter>
</receiver>
</application>
the value specified in the intent filter is incorrect..actual value is
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
and this broadcast can be received for other packages only. Restarted application/package doesn't receive this broadcast.
You should add a data specification to the intent-filter:
<data android:scheme="package" />

Android Notification App

I'm currently working on an android application. I have to log any new installed app name whenever the user is installing/downloading a new third party app. How can I get the notification if the user is installing a new app. Thanks in advance.
Java File
public class ApplicationBroadcastService extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
System.out.print("-------");
}
}
Manifest
<receiver android:name=".applicationlog.ApplicationBroadcastService">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
</intent-filter>
</receiver>
But still I do not enter the onReceive method, when I am installing/uninstalling any app.
Here is the solution:
I did a small change in my Manifest file.
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
Now it's working fine.. :)
Thanks again #willytate
Ajay,
You will need to setup a BroadcastReceiver with an intent filter to receive the following Action: ACTION_PACKAGE_ADDED then from the onReceive() method of the BroadcastReceiver you can launch a Notification.
Take a look at the intent documentation. You are looking for ACTION_PACKAGE_INSTALL (which seems to be never used, see comments) and ACTION_PACKAGE_REMOVED.
You can listen for the android.intent.action.PACKAGE_ADDED intent.

Categories

Resources