I'm trying to use a broadcastreceiver to capture a network change but it doesn't seem to be working at all.
My broadcastreceiver:
public class btReceiver extends BroadcastReceiver {
public btReceiver() {
}
#Override
public void onReceive(Context context, Intent intent)
{
Log.d("BB","Received!");
Toast.makeText(context, "Action: " + intent.getAction(), Toast.LENGTH_LONG);
}
}
The way I call it:
IntentFilter filter = new IntentFilter();
filter.addAction(getPackageName() + "android.net.conn.CONNECTIVITY_CHANGE");
btReceiver myReceiver = new btReceiver();
registerReceiver(myReceiver, filter);
But when I try to toggle let's say wifi, nothing happens at all. I also tried declaring it in the manifest like so:
<receiver android:name=".btReceiver" android:enabled="true" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
But that also doesn't work.
I have declared sufficient permissions in my manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
My test device is running Android 7.1.1, does anyone know why it's not firing?
Apps targeting Android N (Nougat) do not receive CONNECTIVITY_ACTION broadcasts, even if they have manifest entries to request notification of these events. Apps that are running can still listen for CONNECTIVITY_CHANGE on their main thread if they request notification with a BroadcastReceiver.
You should register it programmatically or use JobScheduler, GcmNetworkManager or android-job library
Your package name shouldn't be part of the Intent action. So try with:
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
Even better, there's a constant for that:
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
Related
I make a broadcast call on my Android 10 device, but it returns zero. I tried the suggested solutions but they didn't work. How can I solve it?
manifest
<application
<receiver android:name=".ExampleReceiver" android:exported="true">
<intent-filter>
<action android:name="com.example.myapplication.Test"/>
</intent-filter>
</receiver>
</application>
ExampleReceiver.java
public class ExampleReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String test = "Hello World";
setResultData(test);
}
}
am broadcast -a com.example.myapplication.Test
Broadcasting: Intent { act=com.example.myapplication.Test flg=0x400000 }
Broadcast completed: result=0
Documentation says
If your app targets API level 26 or higher, you cannot use the
manifest to declare a receiver for implicit broadcasts (broadcasts
that do not target your app specifically), except for a few implicit
broadcasts that are exempted from that restriction. In most cases, you
can use scheduled jobs instead.
So you need to register your receiver in code.
registerReceiver(new ExampleReceiver(), new IntentFilter("com.example.myapplication.Test"));
I need a Broadcast Receiver name "NetworkReciver.java" that should execute when Internet is Connected or Disconnected. But it is noit executing.
Manifest permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<receiver android:name=".NetworkReciever" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
In MainActivity.java requested permissions
String[] permissionsRequired = new String[]{Manifest.permission.SET_ALARM,
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE,};
// Since these are Normal Permissions
ActivityCompat.requestPermissions(this, permissionsRequired, 1);
NetworkReciever.java:
public class NetworkReciever extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.i("KAMLESH", "Network REceiver Alarm Set in Hair Style");
}
}
you must register NetworkReciever this broadcast receiver like registerReceiver(new NetworkReciever()); in any activity
From documentation
Apps targeting Android 7.0 (API level 24) and higher do not receive CONNECTIVITY_ACTION broadcasts if they declare the broadcast receiver in their manifest. Apps will still receive CONNECTIVITY_ACTION broadcasts if they register their BroadcastReceiver with Context.registerReceiver() and that context is still valid.
It means you need to register your receiver dynamically, not from manifest. Read Dynamically register/unregister a broadcast receiver in android which will give you the idea about dynamic way.
NOTE : The CONNECTIVITY_ACTION is deprecated since API 28. There are few other options introduced and you can find in the same url CONNECTIVITY_ACTION
I have implemented Job scheduler in my project and it works fine in the case if the app is in background or if the app is killed. But it is not working if the device is rebooted. I have included
JobInfo.Builder mBuilder = new JobInfo.Builder(TASK_ID, mComponentName);
mBuilder.setPersisted(true);
in my builder and
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
in application manifest file.This is how I have added my service to manifest
<service
android:name="com.xxx.xxxx.service.MyJobService"
android:permission="android.permission.BIND_JOB_SERVICE" />
Is there anything else to be included?
Thanks in advance
Register a BroadCastReciever for detecting BOOT_COMPLETED.
<receiver android:name="com.example.startuptest.StartUpBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
And in your BroadcastReceiver:
public class StartUpBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Log.d("startuptest", "StartUpBootReceiver BOOT_COMPLETED");
...
}
}
}
Once a user runs any activity in your app once, you will receive the BOOT_COMPLETED broadcast after all future boots.
I know it is an old question, and you probably already have the solution, but the issue likely was, that JobService caches the UUIDs of the apps that have the permission. You will need to reinstall your app and it is gonna be alright.
Source:
https://android.googlesource.com/platform/frameworks/base/+/5d34605/services/core/java/com/android/server/job/JobSchedulerService.java
You need to call the setPersisted(boolean isPersisted) method.
You can find it in the doc https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#setPersisted(boolean)
I want to mantain a log in my android application , log will contain the Device Started (Bootup) and Device Stop Times. Any Idea how to do this ?
I have to start my application on Bootup , But how to determine that application is started on Bootup ?
I have searched but could not find a better solution.
Use BroadCastReceiver to receive BOOT_COMPLETED broadcast. This broadcast is thrown in device startup
The receiver will be like
<receiver
android:name="ReceiverName"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
You will need to use the following persmission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
now in code write a BroadCastReceiver class like
public class ReceiverName extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// do startup tasks or start your luncher activity
}
}
You can use BroadcastReceiver component for this purpose. Using this you can detect various events of your device like booting.
To Detect Booting process you need to give permission in AndroidManifest.xml as below,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
Then you need to create a BrodacastReceiver which will handle this,
In the onReceive() method the corresponding BroadcastReceiver would then start the event,
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, WordService.class);
context.startService(service);
}
}
Is there a way to detect when a USB flash drive is plugged into an Android device? I'm able to detect an SD card using a broadcast receiver, but it doesn't work for USB. I'd like to avoid polling.
code to register receiver:
private void RegisterUpdateReceiver()
{
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.MEDIA_MOUNTED");
intentFilter.addDataScheme("file");
myReceiver = new MyReceiver();
this.registerReceiver(myReceiver, intentFilter);
}
receiver code:
public class MyReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals("android.intent.action.MEDIA_MOUNTED"))
{
// react to event
}
}
If detecting attaching and detaching the USB will work "android.hardware.usb.action.USB_DEVICE_ATTACHED" can be used.
Make sure the definition of the receiver and the intent filter is added in the manifest as well.
Android, at the SDK level, has no concept of USB drives. There are no rules for where they should be mounted, broadcasts for when they appear/disappear, etc. Perhaps some standardization in this area will come in future Android releases, but it is not there today.
This is the xml version of the receiver in the AndroidManifest.xml:
<receiver
android:name="MyMountBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<data android:scheme="file" />
</intent-filter>
</receiver>
This is how I made it work on Android 6.0.
Basically, what we need to detect is when the drive is mounted which can be done by ACTION_MEDIA_MOUNTED that is "android.intent.action.MEDIA_MOUNTED".
Follow the steps below:
Register a broadcast receiver for "ACTION_USB_DEVICE_ATTACHED"
Register a broadcast receiver for "ACTION_MEDIA_MOUNTED"
Seek permission for using the device when "ACTION_USB_DEVICE_ATTACHED" action is received.
Then you will receive broadcast for "ACTION_MEDIA_MOUNTED"
You can refer: https://developer.android.com/reference/android/content/Intent#ACTION_MEDIA_MOUNTED
Note: Registering broadcast receiver for ACTION_MEDIA_MOUNTED alone does not work. So used broadcast "ACTION_USB_DEVICE_ATTACHED" for permission purpose.