I have to log when the user installs my app, for that I have registered ACTION_PACKAGE_ADDED broadcast in manifest and created a receiver. but the receiver is not triggering after installing the app. I have tried with registering broadcast ACTION_PACKAGE_ADDED instead of PACKAGE_ADDED, and it works. I have read most of the SO questions regarding this and tried, but nothing works for me.I giving my code below
Manifest
<receiver android:name=".AppInstallReceiver">
<intent-filter android:priority="1">
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Recevier
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
String packageName = intent.getDataString();
Log.i("Installed:", packageName + "package name of the program");
}
Log.d(TAG, "Action: " + intent.getAction());
Uri data = intent.getData();
Log.d(TAG, "The DATA: " + data);
}
Is there any problem with this code ?
Related
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>
I'm stuck while working with BroadCastReceiver in which it is invoking only when device is powered on or restarted. I am connecting the USB device using OTG cable. Android system showing USB inserted icon every time but my app not receiving any event.
Let me know what wrong I'm doing.
I am having an application which only has a BroadcastReceiver as below.
public class MountReceiver extends BroadcastReceiver {
private static final String TAG = MountReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
String actionName = intent.getAction();
Toast.makeText(context.getApplicationContext()
, "on Receive", Toast.LENGTH_LONG).show();
extractAllDataFromIntent(intent, context);
boolean isMounted;
if (actionName.equals("android.intent.action.MEDIA_MOUNTED")) {
isMounted = true;
} else {
// actionName.equals("android.intent.action.MEDIA_UNMOUNTED"
isMounted = false;
}
}
private void extractAllDataFromIntent(Intent intent, Context context) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Set<String> keys = bundle.keySet();
Iterator<String> it = keys.iterator();
Log.e(TAG, "Dumping Intent start");
StringBuilder msg = new StringBuilder();
msg.append(TAG + " Seprate app");
while (it.hasNext()) {
String key = it.next();
Log.e(TAG, "[" + key + "=" + bundle.get(key) + "]");
msg.append("[" + key + "=" + bundle.get(key) + "]");
}
Log.e(TAG, "Dumping Intent end");
Toast.makeText(context.getApplicationContext()
, msg, Toast.LENGTH_LONG).show();
//Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
}
}
I have manifest entry for this receiver as follows.
<receiver
android:name="com.example.manmohan.mountreceiverdemo.MountReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<action android:name="android.intent.action.MEDIA_UNMOUNTED" />
<action android:name="android.intent.action.MEDIA_REMOVED" />
<action android:name="android.intent.action.MEDIA_EJECT" />
<action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
<action android:name="android.hardware.usb.action.USB_STATE" />
<data android:scheme="file" />
</intent-filter>
</receiver>
A weird case I see is that receiver is receiving WiFi change events when added WiFi state change callbacks, but still no USB events are getting received.
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
I believe you want to know the external storage is mounted or not at any given point of time:
Is there a way to tell if the sdcard is mounted vs not installed at all?
You have clubbed all the actions together for one receiver. Try keeping separate receiver for each action, and check whether you can see the difference.
I believe you have added a permission in your manifest:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
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>
I have a written a program which intercepts incoming call using BroadcastReceiver. When I start my app, it starts working. Now the problem is when I restart my android phone, this BroadcastReceiver doesn't work. So I am assuming that I need to make a service for this. But I don't know when to start service and where to start BroadcastReceiver.
BroadcastReceiver code -
public class CallInterceptor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Service started", Toast.LENGTH_LONG).show();
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Toast.makeText(context, "Phone is " + state, Toast.LENGTH_LONG).show();
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call from " + phoneNumber, Toast.LENGTH_LONG).show();
//sendSMS(phoneNumber);
//toggle ringer mode
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
toggleMode(am);
}
}
}
AndroidManifest file -
<receiver android:name="com.nagarro.service.CallInterceptor" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</action>
</intent-filter>
</receiver>
I don't think <action android:name="android.intent.action.BOOT_COMPLETED" /> is working.
Also, is it possible to do this without service? Please suggest approach which will help me to intercept call evertime (even after reboot).
You've improperly closed your first action tag. Your receiver section should look like:
<receiver android:name="com.nagarro.service.CallInterceptor" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Also, you are not using a Service. You're using a Broadcast Receiver.
See this page for the basics Application Fundamentals.
I have created a BroadcastReceiver to detect SDCard mount and unmount
event, however, I am not able to receive any events at all:
here's the AndroidManifest.xml:
<receiver android:enabled="true" android:label="SDCardMountReceiver" android:exported="true" android:name="xxx.broadcasts.SDCardBroadcastReceiver">
<intent-filter>
<action android:name="android.content.Intent.ACTION_MEDIA_MOUNTED"></action>
<!-- or <action android:name="android.content.Intent.ACTION_MEDIA_UNMOUNTED" />--></intent-filter>
</receiver>
And the SDCardMountReceiver class:
public class SDCardBroadcastReceiver extends BroadcastReceiver {
public SDCardBroadcastReceiver() {
super();
System.err.println("constructor");
}
public void onReceive(Context context, Intent intent) {
Log.d("SDCardBroadCastReceiver", "receive " + intent.getAction());
System.err.println("jonathan receive " + intent.getAction());
}
}
You also need to set the data scheme to "file".
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
Reference: android-developers thread
The Intent javadoc specifies a different action:name value.
Use "android.intent.action.MEDIA_MOUNTED" instead of "android.content.Intent.ACTION_MEDIA_MOUNTED"
If you register a broadcast receiver programmatically, you must also set the scheme to "file".
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addDataScheme("file");
mContext.registerReceiver(mExternalStorageReceiver, filter);