I'm trying to call my AlarmReceiver which extends BroadcastReceiver. After I run this code, i can't see any of logs. Could you please help me in this issue. :D
Here's where I try to call AlarmReceiber
public void downloadTweets(Context context) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
Intent alarmIntent = new Intent(context,AppService.AlarmReceiver.class);
alarmIntent.putExtra(AppService.SCREEN_NAME, ScreenName);
PendingIntent pi = PendingIntent.getBroadcast(context,0,alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT);//getBroadcast(context, 0, i, 0);
AlarmManager am=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Log.v(LOG_TAG, "shot");
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1, pi);
Log.v(LOG_TAG, "shot finish");
} else {
Log.v(LOG_TAG, "No network connection available.");
}
}
and here's my AlarmReceiver class which is a sub-class of AppService
public static class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.v(LOG_TAG, "AlarmReceiver");
Intent sendIntent = new Intent(context,AppService.class);
sendIntent.putExtra(AppService.SCREEN_NAME,intent.getStringExtra(AppService.SCREEN_NAME));
context.startService(sendIntent);
Log.v(LOG_TAG, "AlarmReceiver done");
}
}
And my manifest has this...
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.nutjane.android.rainalert.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<service android:name=".service.AppService"/>
<receiver android:name=".service.AppService$AlarmReceiver" android:enabled="true"/>
When I run, the log shows only shot and shot finish
Could you please help me solve this issue.
Add:
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in your AlarmReceiver class
Change your manifest file to:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.nutjane.android.rainalert.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".AppService"
android:enabled="true" />
<receiver android:name=".AlarmReceiver" >
<intent-filter>
<action android:name="myintent" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Related
I'm using a BroadcastReceiver to check network state which extends a class rather than initializing BroadcastReceiver. So, is it possible to unregister it anywhere?
If yes then how, if not then what would be the alternate solution of doing that.
I see a solution here but it isn't the actual answer of the question.
Here's my class where I am using BroadcastReceiver.
public class CheckNetworkState extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
/*check WIFI state*/
final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
/*check network state*/
final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isAvailable() || mobile.isAvailable()) {
//Toast.makeText(context, "connection established", Toast.LENGTH_SHORT).show();
} else {
/*shows dialogue*/
Intent alertDialogueIntent = new Intent(context, DialogueUtils.class);
alertDialogueIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alertDialogueIntent);
}
}
}
And here's my Manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera" />
<uses-permission android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Utils.DialogueUtils" />
<receiver
android:name=".Utils.CheckNetworkState"
android:enabled="true">
<intent-filter>
<action
android:name="android.net.conn.CONNECTIVITY_CHANGE"
tools:ignore="BatteryLife" />
</intent-filter>
</receiver>
<activity android:name=".UserAuth.ChangePassword"></activity>
<activity android:name=".QRScanner.CustomScanner" />
<activity android:name=".WaterMetersList.WaterMeters" />
<activity android:name=".UserAuth.Login" />
<activity
android:name=".ControlCharts.Charts"
android:label="#string/title_activity_bottom_navigation" />
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
In your application class create static receiver as follows:
public class YourApplication extends Application {
public static BroadcastReceiver receiver;
public void onCreate() {
super.onCreate();
receiver = new CheckNetworkState();
}
}
After that you can register your receiver anywhere as:
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
this.registerReceiver(YourApplication.receiver, filter);
After that you can unregister it as:
getContext().unregisterReceiver(YourApplication.receiver);
I have an activity on which I am initialising a broadcast receiver. I want this broadcast receiver to to call a function in my activity once it has received the message that the network status of the phone has changed.
Unfortunately it seems like the broadcast receiver never receives this message since the onReceive function is never called.
Here the Receiver:
private BroadcastReceiver NetworkReceiver= new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (!wifi.isAvailable() || !mobile.isAvailable()) {
Log.d("debug"," in the if");
Handler handler = new Handler();
handler.postDelayed(sendUpdatesToUI, 10);
Log.d("Network Available ", "Flag No 1");
}
}
};
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
update();
}
};
private void update() {
Log.d("debug", "got update");
}
I call register it with this line in the onCreate function:
registerReceiver(NetworkReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
and here is my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rtuya.secmere">
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".LoginActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:theme="#style/AppTheme.NoActionBar" />
</application>
</manifest>
EDIT
I have added this in my manifest but still no result:
<receiver
android:name="NetworkReceiver"
android:label="NetworkReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Broadcast receiver also needs to be declared in your manifest.xml
Add
<receiver android:name=".ReceiverName" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
You might want to place receiver in it's own class instead of doing it anonymously.
Don't you have to register receiver in manifest ? Like this:
<receiver
android:name="Receiver"
android:label="Receiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
In my App, I'm using AlarmManager for transmitting keep-alive every day once a day.
I'm using the following code for starting athe AlaramManager
Calendar calendar = DateUtils
.getNextKeepAliveTime(keepAliveHours, keepAliveMinutes);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(),
ALARM_REQUEST_CODE, intent, 0); // PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
Log.d(TAG, "cal: " + calendar.toString());
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
long diff = calendar.getTimeInMillis() - System.currentTimeMillis();
if(diff < 0) diff = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, diff, AlarmManager.INTERVAL_DAY, sender);
When activity goes background, the receiver, AlarmReceiver, does not get any event. But it working successfully when activity in foreground.
Ther receiver declaration in my manifest is -
<receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" />
The manifest is
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.yachtdroid.ba.MainActivity"
android:label="#string/app_name"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.yachtdroid.ba.ui.Settings" >
</activity>
<receiver android:name="MainActivity$ChargingOnReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
<service android:name="com.yachtdroid.ba.services.MailSenderService" />
<service android:name="com.yachtdroid.ba.services.SmsSender" />
<activity android:name="com.yachtdroid.ba.ui.YDSiteView" />
<activity
android:name="com.yachtdroid.ba.ui.BatteryAlertDialog"
android:theme="#style/NoTitleDialog" />
<activity
android:name="com.yachtdroid.ba.ui.ActivityLog"
android:theme="#style/NoTitleDialog" />
<receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" />
<!-- intent-filter>
<action android:name="alarm_action"/>
</intent-filter>
</receiver -->
</application>
The receiver code is
public static class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, intent.getExtras().toString());
if (!inForeground) {
Log.d(TAG, "*** Move app to front!");
Intent it = new Intent("intent.my.action");
it.setComponent(new ComponentName(context.getPackageName(),
MainActivity.class.getName()));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.getApplicationContext().startActivity(it);
}
try {
Toast.makeText(context, "alarm message", Toast.LENGTH_SHORT).show();
MainActivity.instance.sendKeepALive();
} catch (Exception e) {
Toast.makeText(
context,
"There was an error somewhere, but we still received an alarm",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
How it can be solved?
Thanks,
Eyal.
Try to remove the code for the broadcast receiver from the Activity ,make another java file and put it there and also modify your manifest as per that
<receiver android:name=".AlarmReceiver" />
instead of
<receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" />
I'm trying to make an application that when a toggle button is on starts a broadcast that check if battery low and disable wifi. Instead when the toggle is off starts another broadcast and the wifi starts. The problem is that the application crashes when i press the button. this is the code for the toggle and wifi:
public void getToggle(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
PackageManager pm = getPackageManager();
ComponentName compName =
new ComponentName(getApplicationContext(),
LowBatteryReceiver.class);
pm.setComponentEnabledSetting(
compName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
} else {
PackageManager pm = getPackageManager();
ComponentName compName =
new ComponentName(getApplicationContext(),
LowBatteryReceiver.class);
pm.setComponentEnabledSetting(
compName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}
//wifi enable/disable
public class LowBatteryReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
}
}
public class OkBatteryReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
}
}
This is the manifest. The receiver are inside <application> and <activity> tags:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
<application
android:allowBackup="true"
android:icon="#drawable/battery"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".MainActivity"
android:theme="#android:style/Theme.Holo.Light">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".LowBatteryReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
</intent-filter>
</receiver>
<receiver
android:name=".OkBatteryReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.BATTERY_OKAY" />
</intent-filter>
</receiver>
<activity
android:label="#string/app_name"
android:name=".about"
android:theme="#android:style/Theme.Holo.Light">
</activity>
</application>
How can i resolve? I can't find the error..
edit with logcat:
The reference from your manifest to the Receiver classes are not right. Right now, you reference them like they are a normal class within your package. Since they are inner classes, you should reference them like this (see the android:name attribute):
<receiver
android:name=".MainActivity$OkBatteryReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.BATTERY_OKAY" />
</intent-filter>
</receiver>
Inner classes are referenced by the $.
I am testing on an physical device (SAMSUNG ACE GT S5830i)
But I am not receiving the BOOT_COMPLETED intent therefore the service is not Receiver is not starting
This is the code I am using.
public class BootCompleteReceiver extends BroadcastReceiver {
static final String TAG = "InfoService.BroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.e(TAG, "System boot notification received.");
Intent service = new Intent(context, InfoService.class);
context.startService(service);
} else {
Log.e(TAG, "Intent received: " + intent);
}
}
}
This is the Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appengine.paranoid_android.lost"
android:versionCode="2"
android:versionName="1.1">
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity android:name=".InfoSetup"
android:label="#string/activity_name"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LockScreen"
android:label="#string/activity_name"
android:launchMode="singleInstance"
android:clearTaskOnLaunch="true"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
<service android:name=".InfoService"
android:label="#string/service_name"/>
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_CHANGED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<!-- <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> -->
Try to add the full path with the package for <receiver android:name=".BootCompleteReceiver">
<receiver android:name="com.appengine.paranoid_android.lost.BootCompleteReceiver">