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"));
Related
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);
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);
}
}
I want to start my application at startup in Android 4.0. To do that, I wrote some codes and these are completely the same with the #Ahmad's codes (in the answer). However, although I select my application as always, when tablet opens, it asks 'What do you prefer?' (Android's default launcher or my application). I don't want it to ask that question and it must start my application automatically.
Use the BOOT_COMPLETED Intent.
Broadcast Action: This is broadcast once, after the system has
finished booting. It can be used to perform application-specific
initialization, such as installing alarms. You must hold the
RECEIVE_BOOT_COMPLETED permission in order to receive this broadcast.
In your Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Set up a Broadcastreceiver:
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
This is how your BroadcastReceiver could look like:
public class MyBroadcastreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
startActivity(i);
}
}
I am trying to make an App in Titanium which launches on Startup i.e. as the mobile device startsup. I have seen code written at several places which states to do entry into the andsoid manifest file and some code like
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, YourActivity.class);
context.startActivity(myIntent);
}
But i am not able to figure out that where to put this code. In which file ?? and where ?
This 2 answers will do what you need:
Start BroadcastReceiver after some system broadcast:
https://stackoverflow.com/a/7877466/988434
Start BroadcastReceiver on boot:
https://stackoverflow.com/a/8544151/988434
in you BroadcastReceiver you'll implement just call whatever Service/Activty you need.
There an example for that in the question for the 2 answers above.
Tell me if you have any problems unanswered after reading those =].
You have to listen to the BOOT_COMPLETED intent filter. The piece of code you just quoted is from a BroadcastReceiver which will be firing up when the device will boot.
This class has to extend from BroadcastReceiver:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
...
}
}
Then, you have to register that receiver in your manifest file by doing the following:
<receiver
android:enabled="true"
android:name="your_package.BootReceiverClassName"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
Also you need the following permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
By the way you have to make sure that the app is not installed on the SD Card otherwise it won't work (but there are possible workarounds).
I have an Android service which sends broadcast intents. I'm trying to get those intents in another application, which is an Android service. I wrote this in my manifest:
<!-- Service -->
<service android:enabled="true" android:name="...MyService"></service>
<!-- Receiver -->
<receiver android:name="...MyReceiver">
<intent-filter>
<action android:name="..."></action>
<action android:name="..."></action>
</intent-filter>
</receiver>
and this in my MyReceiver class:
public class ScannerBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// Process action.
Log.d(Globals.LOG_TAG, "Intent received.");
...
Unfortunately I never get the onReceive method invoked. Any idea why?
I start this service from another test application, so this is set as an Android library. The service is correctly started but this receiver is receiving nothing. Any idea what I'm doing wrong?
Thanks!
In manifest must be: android:name=".[package].ScannerBroadcastReceiver"
I solved the problem and I suppose it is due to the fact that this project was set as a library. If I don't set it this way the intent is correctly received. I haven't read about this anywhere.