Callback after my application was installed? - android

Is there any callback / receiver / anything that gets called once when my application is installed?

For this, you have to make one application apart from your main application that can keep tracking about the install or uninstalled application from your device.
For this you need to register the Receiver.
<receiver android:name=".AppStatusReceiver" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
</intent-filter>
</receiver>
AppStatusReceiver.java
public class AppStatusReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Intent: " + intent.getAction());
}
}
Note : If you are looking this to be manage from your main application callback then it is no possible.

There is no way for an app to run code when it was installed.
For other apps, you could create a BroadcastReceiver for ACTION_PACKAGE_ADDED, but as the docs explain, this won't work for the app that was newly installed:
Note that the newly installed package does not receive this broadcast.

Related

Setting an app to launch after every reboot

Ideally, we want our app to be the first one to get launched after every reboot, it is a system app so can we set it as default through code?
best you can do is BOOT_COMPLETED handling
manifest
<receiver
android:name="custom.package.BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Java side
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
// your code
}
}
}
but I'm afraid that with this approach you can never be sure if it will be called at first. And I know one "kind" of apps that will be called/opened earlier that any app will receive above broadast - devices Launcher

Google Glass - Autostart Application on Boot

I just received a new google-glass from a company which wants it to support their employees while picking and packing goods in their warehouse. For this reason they need a Server Client application which really isn't the problem.
I never did something with the Glass before and i want to know if it is possible to run a custom Application on boot and to jail the user into it.
Yesterday i rooted the device which gives me full access but i don't know how to go on.
Thank you!
Yes, it is possible.
As you have rooted the device, so you can create system app that can be recognised by the reboot event. Rest of the steps are totally similar to android mobile.
How to do it:
If you need to know the steps you can search on the web or you can try the following:
First, you need the permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Also, in yourAndroidManifest.xml, define your service and listen for the BOOT_COMPLETED action:
<service android:name=".MyService" android:label="My Service">
<intent-filter>
<action android:name="com.myapp.MyService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartMyServiceAtBootReceiver"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Then you need to define the receiver that will get the BOOT_COMPLETED action and start your service.
public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, MySystemService.class);
context.startService(serviceIntent);
}
}
}
And now your service should be running when the phone starts up.

am I able to use android serives in the first time I run my application

I want to get event when I use my program for the firt time so I decided to use this service
public class PackageChangeReceiver extends BroadcastReceiver {
Context context;
#Override
public void onReceive(Context ctx, Intent intent) {
}}
and in the mainfest
<receiver android:name=".PackageChangeReceiver" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
but this services don't work in the fist time I install my application , how can I make it run in the first time ?or it is not possible to do that ?
I have to update the application from eclipse second time to make this service working , what is the problem.
when does android system make its services available to use , I heart onetime that I should run one of my activity to make the service available ,is it true?
Starting Android 3.1, any app that wishes to receive broadcasts must have had one of its UI components (i.e an Activity) run at least once before Android sends any broadcasts to it. This is done for security reasons, to prevent malicious apps from auto launching.

How to start the application after boot complete in android version 3.1 and above?

I got to know boot complete intent is not supported by android version 3.1 and above from here. But in my application. I want to start services automatically in my application after device is rebooted. I do not want user to start my application manually. How can I do it ? Thanks for help in advance.
What makes you think the ACTION_BOOT_COMPLETED broadcast is no longer sent? I make frequent use of it, and so do others. Just be sure you have RECEIVE_BOOT_COMPLETED permission in your manifest.
try the following steps to start the application after boot:
create a class which extends BroadcastReceiver:
public class AutostartService extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("in broad....");
if ((intent.getAction() != null) && (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")))
{
System.out.println("in broadcast receiver.....");
Intent i = new Intent(context, Splash.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
and also add this in android manifest file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".AutostartService" android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Well this is pretty old now and probably most people learned it but still might be useful.
From 3.1+ and on, in order to receive the BOOT_COMPLETED, your app must be started at least once by the user.
NOTE: This rule doesn't apply to /system apps

How to correctly filter Package replaced broadcast

I am trying to catch the package replaced broadcast for my app and only my app, but for some reason in my reciever I am the broadcast for every app that is updated. I thought you only needed to set the intent filter in the manifest file to your app, but maybe I am wrong?
Here's my code(manifest):
<receiver android:name=".UpdateReciever">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" android:path="com.my.app" />
</intent-filter>
</receiver>
Reciever:
public class AppUpdateReciever extends BroadcastReceiver {
#Override
public void onReceive(Context con, Intent intent) {
//code..
}
}
Add this to your onReceive method:
if (intent.getDataString().contains("com.my.app")){
...
}
EDIT:
Note that registering for ACTION_PACKAGE_REPLACED causes your app to be started up every time any app is updated, if it wasn't already open. I don't know how to avoid this before API 12, but in API 12 you can register for ACTION_MY_PACKAGE_REPLACED so you don't have to filter the intent and your app won't be started unnecessarily by other apps being updated.
Alternately, if your code is in a library that's included in multiple apps, or if you just want something that can be copy/pasted between apps without edits:
int intentUid = intent.getExtras().getInt("android.intent.extra.UID");
int myUid = android.os.Process.myUid();
if (intentUid == myUid)
{
...
}

Categories

Resources