Intercept opening android setting of apps with BroadcastReceiver - java

I declared in AndroidManifest permission and receiver
<uses-permission android:name="android.permission.REAL_GET_TASKS"/>
<receiver android:name=".UninstallIntentReceiver">
<intent-filter android:priority="0">
<action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
<data android:scheme="package" />
</intent-filter>
</receiver>
And here is my java class receiver
public class UninstallIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String[] packageNames = intent.getStringArrayExtra("android.intent.extra.PACKAGES");
if(packageNames!=null){
for(String packageName: packageNames){
Log.d("User selected: ",packageName);
}
}
}
When I open settings of other apps it works well - I get notification
(I can see in logs package name of chosen application), but
when I open settings my own application, I don't receive anything

Related

App Service won't Start after reboot by using BroadCastReceiver

I want my service to be started when android is rebooted. I used BroadcastReceiver to do so.
public class autostart extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
MyService1 mYourService = new MyService1();
MainActivity.instance_main.showToast("BroadCast Received...................");
Intent mServiceIntent = new Intent(MainActivity.instance_main, mYourService.getClass());
if (!MainActivity.instance_main.isMyServiceRunning(mYourService.getClass())) {
MainActivity.instance_main.startService(mServiceIntent);
MainActivity.instance_main.showToast("Restarted...................");
}
else {
MainActivity.instance_main.showToast("already running..................");
}
}
}
Receiver in Android Manifest
<receiver android:name=".autostart" 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>
Permissions defined
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Battery optimization ignore permission also requested in OnCreate()
Intent intent=new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:"+MainActivity.instance_main.getPackageName()));
MainActivity.instance_main.startActivity(intent);
But it won't work. Service won't start on reboot. Kindly Suggest me solution.
WE need to register the receiver. Add the following code to OnCreate()
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mService=new Autostart();
registerReceiver(mService,filter);
However after reboot it wont automatically hit.

Oreo - Broadcast Receiver on Boot not Called (No Log in LogCat)

I've some problem with Oreo and Broadcast Receiver on Boot. My method work for earlier version of Android, but with Oreo (Huawei P20 Lite with Pie) it doesn't work.
The Log.e in the Boot Receiver is not displayed in the LogCat and the action in BootService onCreate is not executed (No Log in log cat).
Boot Receiver
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "Boot Receiver : Boot Start Service : AlarmNotification");
if ( Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) ) {
Intent myIntent = new Intent(context, BootService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(myIntent);
} else {
context.startService(myIntent);
}
}
}
}
I've set the permissions in the Manifest file
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service
android:name=".BootService"
android:enabled="true"
android:exported="true">
</service>
BootService
The Boot Service works on earlier versions of Android
public class BootService extends Service {
public BootService() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
//Is fired by the Boot Receiver... on Boot !
Context context = this;
Log.e(TAG, "Start Alarm");
//Do something with the code
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
}
Perhaps I forget something, or do I something wrong ? Can somebody help me on this case, I already tested multiple solutions from StackeOverflow and read the doc, but for me it's not very clear for this case.
Thanks

broadcast receiver to get Toast after install specific app

I am working on Android app.Firstly, I am not familiar with BroadcastReceiver. I need to create an app in which, if install app have specific package like "com.whatsapp" a broadcast receiver will show the Toast
public class PackageAddedReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Package Installed: ", Toast.LENGTH_LONG).show();
}
}
Manifest
<receiver android:name=".receiver.PackageAddedReceiver" android:label="Package added Receiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
How can I add BroadcastReceiver for the specific package?
Try this
public class PackageAddedReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Uri data = intent.getData();
String mypkg="package:com.pck.name";
Log.e("DATA",data+"");
Log.e( "Action: " ,intent.getAction());
if(mypkg.equals(data.toString())){
Toast.makeText(context, "Package Installed: ", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(context, "not match ", Toast.LENGTH_LONG).show();
}
}
Manifest code
<receiver android:name=".PackageAddedReceiver">
<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>
A. Create BroadcastReceiver class (implement onReceive): you can extract the data related to package
import android.content.*;
import android.net.Uri;
import android.util.Log;
public class PackageChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
Uri data = intent.getData();
Log.d(TAG, "Action: " + intent.getAction());
Log.d(TAG, "The DATA: " + data);
}
}
B. Declare receiver with intent-filter in AndroidManifest.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>
The receiver should be registered programmatically as below :
val packageAddedReceiver = PackageAddedReceiver()
val intentFilter = IntentFilter()
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED)
intentFilter.addAction(Intent.ACTION_PACKAGE_FIRST_LAUNCH)
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL)
intentFilter.addDataScheme("package")
registerReceiver(packageAddedReceiver, intentFilter)
for further explanation check CommonsWare answer in the link
Can't receive broadcasts for PACKAGE intents

Broadcast class not recieving broadcast intent for BOOT_COMPLETE

I needed to restore alarm after reboot for this I added this broadcast receiver:
public class ClsRestartAlarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Logging.logMessage("Broadcast");
Intent i = new Intent(context, BootService.class);
context.startService(i);
}
}
}
and registered in manifest like this:
<receiver android:name=".classes.ClsRestartAlarm"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and as result of broadcast I'm doing this:
public class BootService extends IntentService {
public BootService() {
super("boot service");
}
#Override
protected void onHandleIntent(Intent intent) {
AlarmManagerUtils.setStartAlarm();
AlarmManagerUtils.setEndAlarm();
}
}
I guess I'm not receiving BOOT_COMPLETE broadcast in ClsRestartAlarm class, because after restart alarm was not set and I was unable to get notification(the starting alarm start a job scheduler for sending notification and end alarm cancels job scheduler)also I have BOOT_COMPLETE permission like this:
<uses-permission android:name="ANDROID.PERMISSION.RECEIVE_BOOT_COMPLETED"/>
Use WakefulBroadcastReceiver instead. This is my workable solution:
public class BRAutoStart extends WakefulBroadcastReceiver {
private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context ctx, Intent intent) {
_A.APPCTX = ctx.getApplicationContext();
if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){
//code
}
}
}
<receiver android:name=".BRAutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

How to invoke an activity on SMS receive in Android?

My application is receiving an SMS and I want to invoke an activity on receiving that SMS. How do I do this?
create receiver like this
<receiver android:name=".BroadcastServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
</intent-filter>
</receiver>
in class do whatever you want
public class BroadcastServiceReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// do you want
}
}

Categories