I need a Broadcast Receiver name "NetworkReciver.java" that should execute when Internet is Connected or Disconnected. But it is not executing when app is closed or when app is not running.
API 23 and Above
NetworkReciver executes only when app is running.
I want a reciever to monitor the Internet Connection even when the app is closed.
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" />
In Activity
this.registerReceiver(mYBroadCastReceiver, new IntentFilter(
"android.net.conn.CONNECTIVITY_CHANGE"));
NetworkReciever.java:
public class NetworkReciever extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.i("TAG", "Network REceiver Connection Chnaged");
}
}
Related
I have following permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_CACHE_FILESYSTEM" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.REBOOT" />
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Then I have this BroadcastReceiver:
public class StartAfterRebootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Properties.DEBUG) Log.d(TAG, "Receiver started");
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
connectivityManager.registerNetworkCallback(
builder.build(),
new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
if (Properties.DEBUG) Log.d(TAG, "Receiver running after connectivity up");
if (Properties.DEBUG)
Log.d(TAG, "Receiver starts service, probably once");
if (!UpdateService.isCurrentlyRunning()) {
if (Properties.DEBUG)
Log.d(TAG, "Receiver UpdateService is not running, try to start");
UpdateService.startService(context);
}
}
#Override
public void onLost(Network network) {
if (Properties.DEBUG)
Log.d(TAG, "Receiver not running after connectivity lost");
if (UpdateService.isCurrentlyRunning()) {
if (Properties.DEBUG)
Log.d(TAG, "Receiver UpdateService is running, sometimes need to redesigned stop service");
UpdateService.stopService(context);
}
}
}
);
}
}
I have Android TV box with Android 11, exactly I have two boxes from different vendors, on both is there only ethernet, not wifi, but on one of them is all working well, but on the other app start, starts foreground service correctly, when I plug out ethernet cable, service is correcty destroyed, but its all. App dies and over logcat I got this message:
05-18 22:05:03.444 548 852 D ConnectivityService: ConnectivityService NetworkRequestInfo binderDied(NetworkRequest [ LISTEN id=19, [ Capabilities: NOT_RESTRICTED&TRUSTED&NOT_VPN&FOREGROUND Uid: 10027 AdministratorUids: [] RequestorUid: 10027 RequestorPackageName: com.droidlogic.updater] ], android.os.BinderProxy#dc6b02d)
What I am doing wrong. I am sitting here over week and I cant see any mistake. Its system app compiled with android.
Thank you very much.
D
I moved ConnectivityService to Service and it helps.
This question already has answers here:
Oreo BroadcastReceiver SMS Received not working
(6 answers)
I am not able to receive “android.provider.Telephony.SMS_RECEIVED” this broadcast in Android Oreo
(1 answer)
Closed 2 years ago.
How do I properly register a BroadcastReceiver for incoming SMS in an Activity? Registering it in the manifest is not working, presumably due to Background Execution Limits introduced in API level 26.
The required permissions are in the manifest:
<uses-permission android:name="android.permission.RECEIVE_SMS"> </uses-permission>
<uses-permission android:name="android.permission.READ_SMS"> </uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"> </uses-permission>
As well as the Receiver entry:
<receiver android:name=".SmsReceiver" android:exported="true">
<intent-filter>
<action android:name= "android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
The Receiver class:
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context," test sucessfull",Toast.LENGTH_LONG).show();
}
}
I have requested permission at runtime, too:
public void getPermission() {
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECEIVE_SMS},
My_Permission);
}
}
But even registering the Receiver dynamically in an Activity is not working:
sr = new SmsReceiver(); // Broadcast receiver
IntentFilter iff = new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
registerReceiver(sr, iff);
I have a service that is running in the foreground, inside this service i have a broadcast receiver, that listen to the SMS_RECEIVED action.
When the user is inside the application (both the application and the service are in the foreground) everything works well, and i am receiving the intent.
But when the user exists the application (only the service with the broadcast is in the foreground), the service stops when sms is received.
When the service is stopped no error reports are found anywhere (not in the logcat and no crash dialog pops up).
My service with the broadcast:
public class myService extends IntentService {
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(myService.this, intent.getAction(), Toast.LENGTH_SHORT).show();
}
};
#Override
public void onCreate() {
super.onCreate();
IntentFilter i= new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
i.setPriority(999);
registerReceiver(mReceiver, receiverFilter);
startForeground(...);
}
public void onDestroy() {
super.onDestroy();
stopForeground(true);
}
}
And i also have the following permission in my manifest:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
myService is declared like this in the manifest:
<service
android:name=".Services.myService"
android:enabled="true"
android:exported="false" />
I had the same problem and I solved it removing <uses-permission android:name="android.permission.RECEIVE_SMS" />. But removing this permission I can't detect incoming SMS, so I created a class like these Catching Outgoing SMS using ContentObserver and used MESSAGE_TYPE_RECEIVED = 1 instead MESSAGE_TYPE_SENT = 2. You need to add this permission <uses-permission android:name="android.permission.READ_SMS" />.
I have used a Broadcast Receiver which is to be invoked when it finds internet Connection on the android device, to check if the Broadcast Receiver is running or not I have tried printing it in the logcat. I have no message being displayed.
I am invoking a service in my broadcast receiver which is also not getting invoked.
I have seen similar questions on stack overflow but still I am unable to solve the issue.
I am stuck with this since many days. Can anybody help me solve this problem?
This is my Broadcast receiver:
public final class ConnectivityReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.e("BRRRRRRRRRR","works!!!! In Broadcast Receiver...!!!");
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
final PendingIntent wakeupIntent = PendingIntent.getService(context, 0,
new Intent(context, LocationUpdate.class), PendingIntent.FLAG_UPDATE_CURRENT);
final boolean hasNetwork = !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if (hasNetwork)
{
// start service now for doing once
context.startService(new Intent(context, LocationUpdate.class));
// schedule service for every 15 minutes
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_FIFTEEN_MINUTES,
AlarmManager.INTERVAL_FIFTEEN_MINUTES, wakeupIntent);
}
else
{
alarmManager.cancel(wakeupIntent);
}
}
}
Manifest.xml
<receiver android:name="info.androidhive.loginandregistration.ConnectivityReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
Since you are checking connectivity changes, i suggest you can first check if your app has permissions to connect to the internet.
The permission for the app to access the internet would be -
<uses-permission android:name="android.permission.INTERNET" />
Your receiver looks fine. Please, check if your package name is correct. Registering receiver in AndroidManifest.xml should me sufficient, but if it doesn't work for some reason, you can try different solution.
Try to register your receiver programmatically in the following way e.g. in onResume() method in your activity:
#Override
protected void onResume() {
super.onResume();
ConnectivityReceiver receiver = new ConnectivityReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(receiver, filter);
}
You should also unregister your receiver in onPause() method in the activity:
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
If it won't help, you can add new permissions to AndroidManifest.xml file inside the tag.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
I want to receive notification when Mobile network connection is lost or received. Through following code, I can receive notification for Wi-Fi (Data) connection but not for Mobile (Voice) connection.
Manifest :
<uses-permission android:name="android.permission.ACCESS_NETWORK_ST ATE"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".notifier">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE " />
</intent-filter>
</receiver>
</application>
Java :
public class notifier extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast toast = Toast.makeText(context, "Network Changed !!", Toast.LENGTH_LONG);
toast.show();
}
}
Please tell me, how can I receive notification for Mobile Network (Voice).
Pseudocode!!
PhoneStateListener myListener = new PhoneStateListener() {
#Override
public void onServiceStateChanged (ServiceState serviceState) {
// Handle different service states here.
}
};
((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE))
.listen(myListener, PhoneStateListener.LISTEN_SERVICE_STATE);
http://developer.android.com/reference/android/telephony/TelephonyManager.html#listen(android.telephony.PhoneStateListener, int)
Check the permission in the AndroidManifest.xml, first.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
And try this code below.
ConnectivityManager manager = (ConnectivityManager)appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(mobile.isConnected()) {
//Do Sth.
}
If this is not work, check your phone's brand. If the phone's developer blocked some codes or not implement some code, it doesn't work. What is your phone?