Android: receiving intent sent by system ACTION_PACKAGE_RESTARTED - android

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" />

Related

How does PACKAGE_REPLACED works in android? [duplicate]

My application that is not on Play Store verify on the web If there are a new version and download and start it. After the installation I would like to restart the application and I would use a BroadcastRecevier with ACTION_PACKAGE_REPLACED. This is the code :
Broadcast:
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)){
ApplicationInfo app = new ApplicationInfo();
if(app.packageName.equals("it.android.downloadapk")){
Intent LaunchIntent = context.getPackageManager().getLaunchIntentForPackage(app.packageName);
context.startActivity(LaunchIntent);
}
}
}
Manifest:
<receiver android:name="it.android.downloadapk.Broadcast">
<intent-filter>
<action android:name="android.intent.action.ACTION_PACKAGE_REPLACED"></action>
<data android:scheme="package" android:path="it.android.downloadapk" />
</intent-filter>
</receiver>
The problem is that when I install new apk, the Broadcast appears not to start, why ?
see this:
How to know my Android application has been upgraded in order to reset an alarm?
correct fix is that you use the wrong string in the manifest:
http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED
it should be "android.intent.action.PACKAGE_REPLACED" instead.
ok , i see that what i've written is still not enough to try it out, so i will make an exception and publish a whole project just to show that it works:
app code is in a package called "com.broadcast_receiver_test" .
don't forget to run it before testing , or else it won't work on some android versions (i think API 11+) .
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcast_receiver_test" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity android:name=".BroadcastReceiverTestActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
</application>
</manifest>
MyBroadcastReceiver.java:
public class MyBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(final Context context,final Intent intent)
{
final String msg="intent:"+intent+" action:"+intent.getAction();
Log.d("DEBUG",msg);
Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
}
}
please just run it and see that it works perfectly .
EDIT: if your app is for API12 and above, and only wish to handle the case of updating of your app, you can use this intent alone:
http://developer.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED
I put the following receiver in the AndroidManifest.xml
<receiver android:name=".StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
So my app can be launched on update as well as device reboot. Ofcourse as everyone has mentioned that you need API 12+ for MY_PACKAGE_REPLACED.
After hours and hours of searching how to restart your app after it was updated, I finally find why it won't restart.
The app must NOT be launched by Android Studio or other IDE. If I manually install app with its apk and launch it from the current Android device, the app can recognize if there was an update of my application and it restarts correctly.
My user case is a custom launcher that can update by itself launching a PackageInstaller.Session without going to Google Play Store
Here it's the code
Manifest:
<receiver android:name="UpdateReceiver" >
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
UpdateReceiver (Kotlin code):
class UpdateReceiver: BroadcastReceiver() {
companion object {
private val TAG = UpdateReceiver::class.java.simpleName
}
override fun onReceive(context: Context?, intent: Intent?) {
Log.d(TAG, "onReceive ${intent?.action ?: "unknown action"}")
if (intent?.action == Intent.ACTION_MY_PACKAGE_REPLACED) {
//MainActivity = Your first activity
val yourIntent = Intent(context, MainActivity::class.java)
yourIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context?.startActivity(yourIntent)
}
}
}
After two days of struggle and experimentation, I found out the reason.
It turned out I need to carefully read the official documentation.
As said here: https://developer.android.com/reference/android/content/Intent#ACTION_MY_PACKAGE_REPLACED
It turned out the key phrase is:
It does not contain any additional data
those. if you changed the manifest,
then the application will not restart after updating
you are welcome
For anyone trying all the other answers without success, I recommend you restarting the app.
I noticed the app was successfully updated, but it wasn't being launched even though I tried starting the activity in many ways.
I used https://github.com/JakeWharton/ProcessPhoenix in the BroadcastReceiver, so basically in the update you're downloading, add ProcessPhoenix.triggerRebirth(context); in your BroadcastReceiver, or restart the app somehow in there.

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" />

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

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>

android package restarted broadcast not working

I am developing an app which needs a broadcast when app opens every time. I had registered the receiver in manifest like this.
<receiver android:name="package.broadcast.example" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
But i cant able to receive the broadcast. I spent 3 hours on this still i cant find wats the mistake. Can anyone refer me the working example of this broadcast. Thanks.
Restarted Application/Package does not receive broadcast...
check the following link for details
you can check this link
http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_RESTARTED
do u have the following code which extends BroadcastReceiver, if not than try the following code:
public class AutoConnection extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ((intent.getAction() != null)
&& (intent.getAction()
.equals("android.intent.action.PACKAGE_RESTARTED"))) {
Toast.makeText(context, "Pacakge Restarted",
Toast.LENGTH_LONG).show();
}
}
}
and in android manifest file add the following code:
<receiver android:name=".AutoConnection" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
<category android:name="android.intent.category.DEFAULT" />
</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.

Categories

Resources