I try to develop an application that in the future can run a service, the service I want to run when I connect to a network.
At the moment I only need the receiver to print in the log (using react-native log-android) that has been connected or disconnected, or to visualise a toast with a message, all this in the background not in the foreground.
Try the following,
First I put the receiver in the AndroidManifest.xml
<application>
...
<receiver android:name="com.air_fighers_react_native.receiver.NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
Even add the permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Create the receiver folder in java > com > air_fighters_react_native > receiver and inside the NetworkChangeReceiver.java file with the following code:
package com.air_fighters_react_native.receiver;
import android.content.BroadcastReceiver
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class NetworkChangeReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Cambio de conexion.", Toast.LENGTH_LONG).show();
}
}
With this it is supposed that it should be enough, however when running the application there are no problems, but when I close the application and activate and deactivate the WiFi the toast message is not displayed, not even when I have the application in the foreground.
I have already tried changing receiver to:
<receiver android:name=".NetworkChangeReceiver">
And changing NetworChangeReceiver to the same level as MainApplication and MainActivity in folders.
Related
In my Android application I want to run a Service without opening/running my application. For that I have extended BroadcastReciever class. But this BroadcastReceiver class is not being called from AndroidManifest.xml on BOOT_COMPLETE. So please tell what is the problem in my code? Or is there any other way to run a Service without opening my application? I have checked the control flow of my code and whole the code is working perfectly, the problem is that BroadcastReceiver is not being called.
Part of AndroidManifest.xml file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service android:name=".HelloService"
android:exported="false"/>
<receiver android:name=".MyBroadcastreceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
MyBroadcastreceiver.java class
package com.example.abc.project1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
/*this is not being called*/
Intent startServiceIntent = new Intent(context, HelloService.class);
context.startService(startServiceIntent);
}
}
Remove android:exported="false" from the <receiver>. That says you do not want anyone (other than yourself) sending a broadcast to this receiver. As a result, your receiver will be ignored by the system.
Beyond that, you also need an activity and to have run that activity before trying to reboot the device. You may already have that, but I thought that I would mention it for completeness.
I have only one CallBroadcastReceiver which extends BroadcastReceiver and a menifest where its declared.
Still its not showing the Toast while I am placing a outgoing call.
Can you please help?
its showing
[2016-02-27 10:02:20 - OnlyReciever] No Launcher activity found!
[2016-02-27 10:02:20 - OnlyReciever] The launch will only sync the application package on the device!
Code Below -
CallBroadcastReceiver.class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* #author Cosmos
*
*/
public class CallBroadcastReceiver extends BroadcastReceiver
{
public CallBroadcastReceiver() {}
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ind.example.onlyreciever"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-sdk
android:minSdkVersion="22"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name="ind.example.onlyreciever.CallBroadcastReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
</manifest>
For Android 3.1 and higher,
You have to launch one of your activities to get your app out of stopped state before any manifest-registered BroadcastReceiver will work, as detailed in the 3.1 release notes.
When the app is first installed or manually force-closed, it is in "stopped state". In this state, only broadcast intents with the FLAG_INCLUDE_STOPPED_PACKAGES flag will reach your broadcast receivers; this flag is not included in the default system broadcasts, so the app cannot receive them in the stopped state.
Note that "stopped state" is tracked by the package manager and is not the same as "application not running". Once the app is out of stopped state, it will remain so even if you reboot your device and your app is not running.
I am trying to write a code in Android , to create a condition during booting but my condition satisfies everytime ( during booting as well as during running of the device also). I am trying to do is , to execute the condition during the booting only.
My Code :
MainActivity.java
package com.example.bootingtest;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Intent.ACTION_BOOT_COMPLETED!=null) {
Toast.makeText(getApplicationContext(), "Device is booting ...", Toast.LENGTH_LONG).show();
}
}
}
I have given manifest permission .
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
I want to execute this condition only during booting or device start-up but this condition satisfies every time , whenever I open the app.
Please suggest to me how I can run the condition only during the device booting or start-up.
Please help me out.
The Intent.ACTION_BOOT_COMPLETED is a constant, so it's values never changes, that's why you get always true when you activity starts.
What you have to do is declare a BroadcastReceiver on the manifest and implement it, than add a IntentFilter on your declaration to receive the broadcast.
Something like this:
AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
....
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
...
BootReceiver.java:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Do what you need to execute on boot here.
}
}
the class Intent has a string called ACTION_BOOT_COMPLETED that is not null (it has a value). that is what you are checking. what you mean to do is done completely different in a BroadcastReceiver.
In my app, I have a Broadcast Receiver for catching the message sent to my phone
<receiver
android:name="com.qmobile.ows.SMS_Receiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
If I start app with activity GUI, the BroadCast Receiver works normally.
I want to start my application without activity and do not show icon app, so I remove this code below from my activity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
And after that, Broadcast Receiver do not work.
Please help me solve this problem.
This is because Android OS does not allow BroadcastReceiver to receive some important broadcast(android.provider.Telephony.SMS_RECEIVED must be one of it) if the appĀ“s process is not alive.It was designed to against the evil apps. If you have an activity running,your process is alive and so your receiver is allowed to receive the broadcast.
I think you can make a transparent activity and use startService to start a service in background,then finish the activity.As your service is running ,your process is alive,so the Android OS will let you to receive the broadcast.
For Android 3.1 and higher,
You have to launch one of your activities before any manifest-registered BroadcastReceiver will work.
See developer docs specifically the section -
Launch controls on stopped applications for android-3.1
If you are testing Broadcast receiver without an Activity then you should edit your run configuration.
When the Edit configuration dialog appears, select the Do not launch Activity option so that the activity is installed but not launched and click on the Run button
This will launch the application without activity.
This could help you:
Creating and Sending the Broadcast Intent
Having created the framework for the SendBroadcast application, it is now time to implement the code to send the broadcast intent. This involves implementing the broadcastIntent() method specified previously as the onClick target of the Button view in the user interface. Locate and double click on the SendBroadcastActivity.java file and modify it to add the code to create and send the broadcast intent. Once modified, the source code for this class should read as follows:
package com.ebookfrenzy.sendbroadcast;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
public class SendBroadcastActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_broadcast);
}
public void broadcastIntent(View view)
{
Intent intent = new Intent();
intent.setAction("com.ebookfrenzy.sendbroadcast");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
}
.
.
.
}
Creating the Broadcast Receiver
In order to create the broadcast receiver, a new class needs to be created which subclasses the BroadcastReceiver superclass. Create a new project with the application name set to BroadcastReceiver and the company domain name set to com.ebookfrenzy, this time selecting the Add No Activity option before clicking on Finish.
Within the Project tool window, navigate to app -> java and right click on the package name. From the resulting menu, select the New -> Other -> Broadcast Receiver menu option, name the class MyReceiver and make sure the Exported and Enabled options are selected.
Once created, Android Studio will automatically load the new MyReceiver.java class file into the editor where it should read as follows:
package com.ebookfrenzy.broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
throw new UnsupportedOperationException("Not yet implemented");
}
}
As can be seen in the code, Android Studio has generated a template for the new class and generated a stub for the onReceive() method. A number of changes now need to be made to the class to implement the required behavior. Remaining in the MyReceiver.java file, therefore, modify the code so that it reads as follows:
package com.ebookfrenzy.broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast Intent Detected.",
Toast.LENGTH_LONG).show();
}
}
The code for the broadcast receiver is now complete.
Configuring a Broadcast Receiver in the Manifest File
In common with other Android projects, BroadcastReceiver has associated with it a manifest file named AndroidManifest.xml.
This file needs to publicize the presence of the broadcast receiver and must include an intent filter to specify the broadcast intents in which the receiver is interested. When the BroadcastReceiver class was created in the previous section, Android Studio automatically added a <receiver> element to the manifest file. All that remains, therefore, is to add an <intent-filter> element within the <receiver> declaration appropriately configured for the custom action string:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ebookfrenzy.broadcastreceiver.broadcastreceiver" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action
android:name="com.ebookfrenzy.sendbroadcast" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>
With the manifest file completed, the broadcast example is ready to be tested.
add DEFAULT category in your intent filter
<category android:name="android.intent.category.DEFAULT" />
Ok. Here's what worked for me. I created a launcher activity. Removed the call to setContentView(R.layout.activity_main)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
finish();
}
I am trying to start a new app, but i need to know about a change in the phone state...
for some reason (i am new to this) i cant catch the broadcast of the change.
thats my code:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG","yyyyyyyyyyyy");
}
}
and thats my manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
as you can see... very simple.
but i cant see the Log in the onReceive on my LogCat.
any one knows why?
Thanks!
Due to this and this, your BroadcastReceiver won't receive anything. The solution is to design a simple activity for your app that starts at least once after installation, before it starts receiving.
In order for a Manifest based Broadcast Receiver to get registered, you need to run your app by opening it from the app drawer. If you don't do that, then the receiver will not be called by Android. So basically, you need at least one Activity that can start - it does not have to do anything, it just has to be started at least once. Did you do that?
This is true after Android 3.0+