Android: How to detect if a broadcaster application is deleted? - android

I have 2 applications. One of them is doing broadcast custom strings continously and the other one is receiving. I have to be notified and delete some datas in the reciever application when the broadcaster application is deleted. Is there a method like onDelete() or something like that? How can I do this?

Yeah! There's an intent called ACTION_PACKAGE_REMOVED that you can listen for.
Add this inside <application> in your manifest: (don't forget to change the package name)
<receiver android:name="com.arjnklc.receiverapp.UninstallReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
Then you need to create the class mentioned above.
public class UninstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getData().getSchemeSpecificPart() == "com.arjnklc.broadcasterapp")
cleanUpEverything();
}
}

Not exactly sure when you want to do but from what I understand, you want your second application to know when the first application is deleted?
If that's the case, do this:
In AndroidManifest.xml, you MUST have a new BroadcastReceiver because this receiver used a different data scheme:
<receiver
android:name=".PackageReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Then your BroadcastReceiver:
public class PackageReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
Log.d(TAG, "ACTION_PACKAGE_REMOVED");
String data = intent.getData().toString();
// data string has the package name
// if that is your package name, your first app was uninstalled
}
}
}
Just make sure, it's a separate BroadcastReceiver. It can not be combined with any other Receiver or the other actions will stop working.
Hope this works.

Related

Implicit Broadcast Reciever isn't calling

I searched the web for alot of time and I don't understand why my custom broadcast
isn't working.
<receiver
android:name=".myservice.MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
<action android:name="android.intent.action.BATTERY_CHANGED"/>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
</intent-filter>
</receiver>
I don't it not recieve when I reconnet and disconnect the charger.
I did this for making thing simpale
public class MyReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context,"Battery", Toast.LENGTH_SHORT).show();
Log.i("Recive", "Yes");
}
}
From docs:
ACTION_BATTERY_CHANGED
Broadcast Action: This is a sticky broadcast containing the charging state, level, and other information about the battery. See BatteryManager for documentation on the contents of the Intent.
You cannot receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver(). See ACTION_BATTERY_LOW, ACTION_BATTERY_OKAY, ACTION_POWER_CONNECTED, and ACTION_POWER_DISCONNECTED for distinct battery-related broadcasts that are sent and can be received through manifest receivers
So, you cannot use this BroadcastReceiver decalred in Manifest, only registering explicitly from your context.
Also, your power connection BroadcastReceiver seems correct. Try to separate it into another BroadcastReceiver, maybe action ACTION_BATTERY_CHANGED is interfering with other actions.
This is my declared BroadcastReceiver which I use and it's working in my app.
<receiver android:name=".PowerConnectionBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
PowerConnectionBroadcastReceiver
public class PowerConnectionBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "PowerRcvr";
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
Log.d(TAG, "Device is charging");
} else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
Log.d(TAG, "Device is NOT charging");
} else {
Log.d(TAG, "Unable to check if device is charging or not");
}
}
}
NOTE: This code is working on Android 8 with targetSdkVersion 25 or lower.
In targetSdkVersion 26 or higher most of BroadcastReceivers doesn't work through Manifest due to background limitations. Here are documentation (thanks to Pawel) about that. So your IntentFilters wont work. To keep it working you can download your targetSdkVersion to 25 or lower.

How to insert data in sqlite once in a day at specific time in android in backgroud?

My task is to insert or update some specific data in sqlite based on some condition at 10:00 daily in background. I am stuck. Please Help.
Make YourReceiver.java file,
class YourReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches(Intent.ACTION_TIME_TICK)) {
if (check_your_time_with_system_time) {
// update your DB here
}
}
}
}
Register your Receiver in manifest.xml.
<receiver android:name=".YourReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_TICK"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
The onReceive() method of YourReceiver will be called every minute.

Priority two apps use bootcompletedReceiver on android

recently I use BOOT_COMPLETED 2 app (A app, and B app)
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
A app is activity and B app is service app
when my device boot,
first A app launch and B app launch
so, show B app screen.
I want
first B app launch and A app launch showing A app screen
perhaps, Can I give BOOT_COMPLETED Priority is possible?
finally, I want when I boot my device, show A app screen
Thanks!
add
I try
B app(service)
public class BootCompletedReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) {
Intent i = new Intent("A app package name.BOOT_COMPLETED");
context.sendBroadcast(i);
}
}
}
<receiver android:name=".BootCompletedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
A app(activity)
public class BootSendReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context,Intent intent) {
if( intent.getAction().equals("B app packagename.BOOT_COMPLETED"));
Intent i = new Intent (context, MainActivity.class);
context.startActivity(i);
}
}
<receiver android:name=".BootSendReceiver">
<intent-filter>
<action android:name="blackeyeonandroid.iosystem.co.kr.simpleserviceexample.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
and I try boot .. but showing B app screen
I think you can not control that..
Instead, you can make APP1 start the APP2. This way, only APP1 receives the BOOT_COMPLETE message. Then, APP1 is responsible to send a new intent to start APP2:
Maybe, you can do as follows (note that APP2 does not receive android's default BOOT_COMPLETED message):
APP1
Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".AppToStartFirstBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Receiver
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent("com.example.mytestapp.BOOT_COMPLETED");
context.sendBroadcast(i);
}
}
APP2
Manifest:
<receiver android:name=".AppToStartLaterBroadcastReceiver">
<intent-filter>
<action android:name="com.example.mytestapp.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Receiver
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.example.mytestapp.BOOT_COMPLETED")) {
// Do what you want in secundary APP
}
}
Note
This is an suggestion and you should adjust to your case. Since I don't have more details about your code, you may need to modify it to your case.. But you can use the idea.

Android BroadcastReceiver in separate project

I'm developing two Android applications.
The first one calls broadcast receiver from activity.
The second one contains broadcast receiver that is called from first application.
I've manage to do broadcast call when it is in the same application with caller activity.
But when I take receiver to separate project it doesn't work. What should I change?
This is how I register receiver:
<receiver
android:name=".TestReceiver"
android:enabled="true"
android:exported="true"
android:process=":deltaFO">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.myapp.intent.action.FILE_OPERATION" />
</intent-filter>
</receiver>
This is how I send intent
Intent intent = new Intent("com.myapp.intent.action.FILE_OPERATION");
intent.putExtra("operation", operation);
context.sendBroadcast(intent);
This is class that receives intent:
public class TestReceiver extends BroadcastReceiver{
public final String TAG="TestReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG,"EXTERNAL BROADCAST...");
}
}

BroadcastReceiver is not working

I have implemented this broadcast reciever:
public class ServiceManager extends BroadcastReceiver {
private final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";
private final String BOOT_ACTION_FIRST_LAUNCH = "android.intent.action.PACKAGE_FIRST_LAUNCH";
private final String BOOT_ACTION_RESTARTED = "android.intent.action.PACKAGE_RESTARTED";
#Override
public void onReceive(Context context, Intent intent) {
// All registered broadcasts are received by this
String action = intent.getAction();
if (action.equalsIgnoreCase(BOOT_ACTION) || action.equalsIgnoreCase(BOOT_ACTION_FIRST_LAUNCH) ||
action.equalsIgnoreCase(BOOT_ACTION_RESTARTED)) {
// TODO: Action
}
}
}
AndroidManifest.xml
<receiver android:name="package.service.ServiceManager" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH" />
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
</intent-filter>
</receiver>
The BOOT_COMPLETED action is working right, but, the PACKAGE_FIRST_LAUNCH and PACKAGE_RESTARTED are not working. I need to launch my broadcast receiver when I launch my app, that's why I'm using these actions. But, when I launch or restart the app, the receiver is not working. It only works when I restart my mobile phone. Are there something wrong in my source?
FYI: PACKAGE_FIRST_LAUNCH is only sent to the installer package, i.e. whatever you used to install the application - for most end users that would be Android Market.
Edit:
Oh, and for "PACKAGE_RESTARTED", break that one out into its own <intent-filter> and add a
<data android:scheme="package"/>
since that one comes with an URI and an explicit scheme.
Logically it seems that PACKAGE_FIRST_LAUNCH will be broadcasted once your app is run for the first time after boot/reboot. And PACKAGE_RESTARTED should be broadcasted if your application activity stack is removed and then your app is clicked to start again (like restart).
However, you may simply achieve this by broadcasting a custom action string when ever your app is launched (perhaps from your first activity).
Manifest:
...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></>
...
<receiver android:name=".AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
...
Receiver:
package YourPackage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AutoStart extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
// Your code
}
}
}
The intent android.intent.action.PACKAGE_FIRST_LAUNCH is introduced in Android API Level 12. If you are using lesser API Level it will not work. So change your project settings accordingly.

Categories

Resources