When a new application is installed , my BroadcastReceiver gets package data with a simple filter :
filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
filter.addDataScheme("package");
receiver = new newPackageReceiver();
registerReceiver(receiver, filter);
...
public class newPackageReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
final String info = intent.getData().toString();
...
}
}
BroadcastReceiver is called with most of devices... However, with this device (only in japanese, sorry), onReceive is never called.
model: sony SO-03D
android-version: 4.0.4
No update available for the device.... any ideas?
Maybe this helps.
Try to add the receiver to the manifest.
<receiver android:name=".YourReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
And in YourReceiver class:
public class YourReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String act = intent.getAction();
if (Intent.ACTION_PACKAGE_ADDED.equals(act) || Intent.ACTION_PACKAGE_REMOVED.equals(act)) {
//Do what you want
}
}
I hope this would help, but it seems that the problem is with this device's rom.
Related
I am listening for "android.net.conn.CONNECTIVITY_CHANGE" in my app with a broadcast receiver. It does work, but the only problem I have is that there is a huge delay in receiving this broadcast. It takes between 1-2 seconds for the app to receive this broadcast. Am I doing something wrong?
My Manifest entry -
<receiver android:name=".ConnectivityBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
and ConnectivityBroadcastReceiver class -
public class ConnectivityBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG,"Broadcast received");
final String action = intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityUtil.checkConnectivity(context);
}
}
}
I added a receiver to listen when app is installed. But it is not working. Here is my code in AndroidManifest.xml
<receiver android:enabled="true"
android:exported="true"
android:name="com.bsp.iqtest.reiceiver.IQTestReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
Here is my code in MainActivity (launcher activity) , function onCreate.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IQTestReceiver br = new IQTestReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
intentFilter.addDataScheme("package");
registerReceiver(br, intentFilter);
}
Here is my code in IQTestReceiver (this class is written in other file)
public class IQTestReceiver extends BroadcastReceiver {
public IQTestReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
String packageName=intent.getData().getEncodedSchemeSpecificPart();
Log.e("HELLO",packageName);
}
}
I set a breakpoint in onReceive function , but it doesn't run when i debug.
Thanks for your helping.
You can not receive PACKAGE_ADDED or PACKAGE_REPLACED for your own app, if that is what you're trying.
"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."
See http://developer.android.com/reference/android/content/Intent.html
set your broadcasrt in manifest like this
<receiver
android:name=".IQTestReceiver"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="check_values"/>
</intent-filter>
</receiver>
and send the broadcast like this.....Intent it1=new Intent(Intent.ACTION_USER_PRESENT);
it1.setAction("check_values");
it1.putExtra("data_key1",message);
sendBroadcast(it1);
and in on receive would be like this....
#Override
public void onReceive(Context context, Intent intent)
{
data1=intent.getStringExtra("data_key1");
System.out.println("ffffff11" + data1);
}
I have an application that use parse push notification service. Here is the class that I'm using for receiving notification:
public class MessageReceiver extends ParsePushBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//...some code in here
}
And I also register this custom receiver in my manifest:
<receiver
android:name="com.package.MessageReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
Everything is fine with notification system. After a notification have received, I want to update the running activity UI accordingly (like showing an icon for new notification) but I don't have access to the activity object in onReceive method. What is the best practice to do that? I couldn't use the context object in this matter.
Thanks
I believe you can accomplish this by using a BroadcastReceiver.
You would define the receiver in the activities you want to have access to,
See snippet below.
public MyActivity extends Activity
{
//... code
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//... update ui here
}
};
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction("suitablename");
registerReceiver(receiver, filter);
super.onResume();
}
#Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
//... code
}
Then in your custom receiver send the broadcast.
public class MessageReceiver extends ParsePushBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//...some code in here
Intent intent = new Intent();
intent.setAction("suitablename");
context.sendBroadcast(intent);
}
}
Don't forget to update the manifest
<activity
android:name=".MyActivity" >
<intent-filter>
<action android:name="suitablename"></action>
</intent-filter>
</activity>
When I'm setting my broadcast receiver as anonymous class his never called
but when I'm setting it as class and declare it on android manifest its work fine
i want the ability of register and unregister the broadcast receiver dynamically
why its won't working
here is my code:
public class AppChangedProbe extends Probe.Base implements Probe.ContinuousProbe{
private BroadcastReceiver appReceiver;
#Override
protected void onEnable() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
appReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED))
Logger.i(getClass(), "App Removed");
if (intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED))
Logger.i(getClass(),"App Updated");
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED))
Logger.i(getClass(),"App Added");
}
};
getContext().registerReceiver(appReceiver, filter);
}
#Override
protected void onDisable() {
getContext().unregisterReceiver(appReceiver);
}
#Override
protected boolean isWakeLockedWhileRunning() {
return false;
}
}
the Probe.Base and Probe.ContinuousProbe are FUNF project jars.
i set this permissions on the manifest:
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_CHANGED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_ADDED" />
when i use the broadcast receiver as class its work
here is the code that work:
public class AppChangedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED))
Logger.i(getClass(),"App Removed");
if (intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED))
Logger.i(getClass(),"App Updated");
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED))
Logger.i(getClass(),"App Added");
}
}
and in the manifest:
<receiver android:name =".sensors.EventBaseProbes.AppChangedReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<action android:name="android.intent.action.PACKAGE_CHANGED"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package"/>
</intent-filter>
the occurred is that when i use other actions its work in both ways.
for example if i replace the filter on the example to this filter
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
the receiver works fine and get called when the screen is on
are they 2 kinds of action:
1) need to be declared on manifest
2) don't need to be declared on manifest
I have been dealing with the same problem recently. Here is how you should do.
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
Since you have <data android:scheme="package"/> inside your AndroidManifest file. You should have it programatically too. Otherwise it won't work.
And lastly, you don't have to add any permission. These Broadcasts doesn't require permission.
I'm trying to track an APK install. When a user lands on the downloadpage (not the store), he is coming from a specific source. When user clicks on download, the APK will be installed. After it's installed, I need to map the install to the source the user was coming from before installing. Is there any good way to do this?
My plan so far: Save the user IP and screen resolutions on the download page to a database. After install, pass IP and screen resolution to the server and map with the row in the database. Is this a good way of doing this?
Hope you guys can help me.
You just need to write a BroadcastReceiver for this which can receive the PACKAGE_ADDED and PACKAGE_INSTALL Intent:
InstallBroadcastReceiver.Class
public class InstallBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_PACKAGE_ADDED)
||action.equals(Intent.ACTION_PACKAGE_INSTALL)){
notifyServerForApplicationInstall(context, intent);
}
}
private void notifyServerForApplicationInstall(Context context,Intent intent){
//send the data to your server here
}
}
Register the receiver in AndroidManifest file
<receiver
android:name=".InstallBroadcastReceiver"
android:exported="false"
<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>
Don't forget to give this permissions in manifest :
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
I have prepared a BroadcastReceiver class :
public class newPackageReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("DEBUG"," test for application install/uninstall");
}
}
In the main activity, I first register a new receiver object, then instanciate button for application install.
public void onCreate(Bundle savedInstanceState) {
...
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
filter.addAction(Intent.ACTION_PACKAGE_INSTALL);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
receiver = new newPackageReceiver();
registerReceiver(receiver, filter);
...
dlButton.setText(R.string.dl_button);
dlButton.setOnClickListener(new AppliDownloadOnClickListener(this ));
#Override
public void onDestroy(){
unregisterReceiver(receiver);
super.onDestroy();
}