BroadcastReceiver not receiving boolean value after AlarmManager finishes set time - java

I am using a BroadcastReceiver to enable a button when it is triggered. I have an AlarmManager that runs for a certain amount of time. When the time has elapsed, it sends a broadcast like this:
Intent i = new Intent("polarCap1Status");
i.putExtra("polarCap1Stat", true);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);
I use this code to receive the broadcast:
//broadcast receiver to allow button to be clicked again
mMessageReceiver1 = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
clickOnOff1 = intent.getBooleanExtra("polarCap1Stat", false);
polarCap1.setEnabled(clickOnOff1);
updateScores();
resources.edit().putBoolean("pc1Set", false).commit();
}//end onReceive function
};
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(mMessageReceiver1, new IntentFilter("polarCap1Status"));
I have the receiver setup in my manifest like so:
<receiver android:name="com.twentytwentythree.sab.SetupTimerPC1" >
<intent-filter>
<action android:name="polarCap1Status" />
</intent-filter>
</receiver>
SetupTimerPC1 is the name of the class that is sending the broadcast. It is sending it to another class called runGraphics.
Please let me know if you need any more information.
Edit:
I added an intent-filter to the receiver in the manifest but it has not changed anything.

Related

Broadcast receiver. I want to move to another activity through a broadcast receiver after the charger is disconnected

I'm making a university project where I have to make a charger alarm. I select a ringtone, enter a pin, then plug in a charger. When I plug out, it moves to another activity where the phone starts ringing, the screen is locked and can only proceed if provided a correct pin code.
I'm stuck at the point where I have to move from one activity to another when the charger disconnects.
My code is not working. This is the receiver in the manifest file:
<receiver android:name=".MainActivity$PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
This is the broadcast receiver I made in the MainActivity:
public class PowerConnectionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context, "The device is not charging", Toast.LENGTH_SHORT).show();
Intent myintent = new Intent(MainActivity.this,setter.class);
startActivity(myintent);
}
}
Please see:
You cannot receive this through components declared in manifests, only
by explicitly registering for it with...
https://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED
And did you set the corresponding permission?
<uses-permission android:name="android.permission.BATTERY_STATS"/>
Your intent has not correct parameters.
Intent myintent = new Intent(MainActivity.this,setter.class);
Change should be:
Intent myintent = new Intent(context, MainActivity.class)
Make sure your <receiver></receiver> tags are out of the activity tag in Manifest. Place them in application level.

Broadcast receiver not firing

I'm programming an application that will fire a specific method once it hits a certain date / time. However, my broadcast reciever is not firing...i can see that the alarm is set.
my method within an activity:
private void setAlarm(Date date){
Intent activate = new Intent(this, Alarm.class);
AlarmManager alarms ;
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(date.getTime());
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, activate, 0);
alarms = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Log.d("ALARM","Setting alarm");
alarms.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);
Log.d("ALARM","Setting time "+cal.getTimeInMillis());
}
My BroadcastReciever:
public class Alarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("ALARM", "RING RING");
}
}
My manifest within the <application> tag:
<receiver android:name=".Alarm" android:process=":remote" >
<intent-filter>
<action android:name="com.package.feature.subpackage.arrangealarm.ALARM_ACTION"/>
</intent-filter>
</receiver>
Does anyone have any idea why this is not firing?
There seems to be nothing wrong with your code (I copied the snippet and had Android Studio generate a BroadcastReceiver for which I set android:process=":remote" just like you do).
When I run the app, the BroadcastReceiver fires but I can only see the respective Logcat entry when I select "No Filters" in the Logcat settings (on the right side), not when I choose "Show only selected application".
This happens because you use android:process=":remote". The BroadcastReceiver will run in another process than the "main" application process, so the "RING RING" will appear under another process id.

How can i change incoming call screen of phone in java?

I want to change incoming call screen to my own layout. I used a sample to do this for me. but it sometimes appear(when calling) and sometimes not. I do not know how to solve this problem that my own layout appear every time.
This is my codes.
public class PhoneStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
Intent i = new Intent();
i.setClass(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
and this is the manifest.
<receiver android:name="com.example.changescreen.PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Try adding a delay before starting your activity.
The Problem is your activity is being overlapped by the default activity

BroadcastReceiver isn't receiving ACTION_SCREEN_OFF intent

I have created a BroadcastReceiver that should listen for the "ACTION_SCREEN_OFF" intent.
public class ExampleReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// run a service..
}
}
I have registered it in the Manifest:
<receiver android:name=".path.to.ExampleReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SCREEN_OFF"></action>
</intent-filter>
</receiver>
But when the screen is turned off, the onReceive() method is not called. What else do I need to do to get this functionality?
ACTION_SCREEN_OFF is not sent to receivers registered in the manifest. It is only sent to receivers registered via registerReceiver() from a running component.

OnBootReceiver works, but doesn't find AlarmReceiver

I'm trying to set up an alarm receiver right after booting. Therefore, i have an OnBootReceiver that should register the alarm. The onBootReceiver works and it gets called, but somehow it cannot find my AlarmReceiver class.
OnBootReceiver which succesfully starts after booting:
public class OnBootReceiver extends BroadcastReceiver {
private static final String TAG = "OnBootReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "called");
Intent i = new Intent(context, com.packagenames.AlarmReceiver.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, 30);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), time.getTimeInMillis(), pi);
}
}
As you you can see, it configures the alarm tries to invoke com.packagenames.AlarmReceiver.class. This class exists and is located in the same package:
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "alarm received");
Intent i = new Intent(context, com.packagename.DataService.class);
i.putExtra("action", "process");
context.startService(i);
}
}
Unfortunately, i get the following error:
02-03 09:22:25.344: W/ActivityManager(103): Unable to start service
Intent { flg=0x4 cmp=com.phonegap.packagename/.AlarmReceiver (has extras)
}: not found
The Android Manifest looks like this
<application>
// activities etc
<receiver
android:name="com.phonegap.packagename.OnBootReceiver"
android:enabled="true"
android:exported="false"
android:label="OnBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name="com.phonegap.packagename.AlarmReceiver"
android:enabled="true"
android:label="AlarmReceiver">
<intent-filter>
</intent-filter>
</receiver>
</application>
Do you see a mistake? Maybe I forgot something?
Thanks
edit: in the manifest, I added
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
in order to make the OnBootReceiver work. Do I need something similar for the alarm?
Shouldn't you use getBroadcast instead of getService when creating a pending intent ?
The whole receiver stuff ONLY works if you application is NOT installed on a SD card. Add this to your manifest file to do so:
android:installLocation="internalOnly"

Categories