Broadcast Receiver not receivng Broadcasts - java

I seem to be having trouble getting my onReceive class receive any broadcasts I send out. Im not sure if its my code thats the problem or its a problem with the Android Manifest.
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("BROADCAST_RECEIVED", intent.getDataString());
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP, "");
wakeLock.acquire();
wakeLock.release();
context.startActivity(intent);
}
}
public void setDayOfWeekAlarm(DayOfWeek day){
long alarmInMili = 0;
Intent intent = new Intent(context,AlarmScreenActivity.class);
alarmInMili = System.currentTimeMillis() + 1000*10;
Log.i("REGISTER ALARM", String.valueOf(alarmInMili));
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() +
10 * 1000,pi);
}
AndroidManifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.brianlindsey.alarm"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name="com.brianlindsey.AlarmReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>

In addition to Gabe's answer, the Intent that you are using in setDayOfWeekAlarm() points to AlarmScreenActivity. That is not your BroadcastReceiver, nor is it any other component registered in your manifest.

BOOT_COMPLETED receivers cannot recieve a broadcast until an activity in the apk has been launched at least once. Its a weird rule google added to prevent people from downloading an app by mistake and having it run at boot.

Related

When I restart the phone I set the alarm but it does not run

I have an app developed in java whose function is to capture the location when the app is active, background, destroyed and when the phone restarts
I already managed to capture the location when the app is active, background and destroyed but I am trying to do it when the phone reboots and create the task to capture but it says that the task was configured well but it does not execute it every 1 minute
My Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.rutas">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.Rutas"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyForegroundServices"></service>
<receiver android:name=".MyBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
My Receiver
public class MyBroadcastReceiver extends BroadcastReceiver {
GPSTracker gps;
Context alarmContext;
#Override
public void onReceive(Context context, Intent intent) {
Log.e("onReceive", " Init Receive" );
alarmContext = context;
gps = new GPSTracker(context);
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Log.e("Location", "Lat: " + latitude + "\n Long: " + longitude);
if(intent.getAction() != null){
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Log.e("Boot Completed", "Boot Completed");
setAlarmOnBoot();
Intent serviceIntent = new Intent(context, MyForegroundServices.class);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
context.startForegroundService(serviceIntent);
}else{
context.startService(serviceIntent);
}
}
}
}
public void setAlarmOnBoot(){
Log.e("Alarm Boot", "Initialize task");
try{
AlarmManager alarm = (AlarmManager) alarmContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(alarmContext, MyBroadcastReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(alarmContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 1000*60*1, pIntent);
Log.e("Success", "Alarm has been configured successfully");
} catch (Exception ex){
Log.e("Failed Try Receiver", ex.getMessage());
}
}
}
I am testing the app on a samsung A13 with android 12

Alarm Manager does not work in background

I used alarm manager in my program,but for the first time I run it, It just worked when my program was open and when it was close it did not work in the background, but in the second time it worked in the background too.
MainActivity :
public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
Intent intent;
PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
intent = new Intent(MainActivity.this, AlertReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+60000, pendingIntent);
}
}
AlertReceiver :
public class AlertReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Log", "onReceiver!!");
}
}
AndroidManifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.yadame">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<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=".MainActivity" android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlertReceiver"/>
</application>

Schedule alarm Android app

I have some trouble ring an alarm on android-studio app. First, I have a method, called with a button ( onClick), then I create alarmManager in this, and try to call a class AlarmReceiver to ring the alarm. The problem is that the program running never go in my AlarmReceiver class, and I don't understand why. Let me show you the code:
public void sendMessage(View v) {
long time = currentTimeMillis();
Toast.makeText(this, "ALARM ON", Toast.LENGTH_SHORT).show();
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 58);
Toast.makeText(MainActivity.this, "bjr", Toast.LENGTH_SHORT).show();
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);}
This is the method called when click on button, and now this is the AlarmReceiver:
public class AlarmReceiver extends WakefulBroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Alarm! Wake up! Wake up!", Toast.LENGTH_LONG).show();
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null)
{
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
}
}
So, when I run the app, then click on button, the word " ALARM ON" and "bjr" is printed, but the text so in AlarmReceiver "Alarm! Wake up!" Is never printed, so the app never go in this class, I need to know why.
My android manifest :
`<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.beantunes.myapplication">
<uses-feature
android:name="android.hardware.sensor.accelerometer"
android:required="true"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".vue.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".vue.AlarmReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<activity android:name=".vue.DisplayMessageActivity"></activity>
</application>
`

WakefulBroadcastReceiver is not working on Android 6.0

I want to implement a background task in my android application. For Android 5.0 and lower, my code suffices with a simple BroadcastReceiver, where I can easily set an repeating alarm with
setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * seconds, pendingIntent);
On Android 6.0, however, I found out to replace my BroadcastReceiver with WakefulBroadcastReceiver and
setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
It is still not working at all and I don't know what my mistake is..
Here my code:
public class AlarmManagerBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
mContext = context;
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
//Acquire the lock
wl.acquire();
callback = new GPSActivityCallback() {...}
if(isConnectedToInternet()) {
gpsService = new GPSService(callback, context);
gpsService.refreshLocation();
Bundle extras = intent.getExtras();
StringBuilder msgStr = new StringBuilder();
if (extras != null && extras.getBoolean(REPEAT, Boolean.FALSE)) {
//Make sure this intent has been sent by the one-time timer button.
}
Format formatter = new SimpleDateFormat("hh:mm:ss a");
msgStr.append(formatter.format(new Date()));
}
else {
Toast.makeText(context, "Internetverbindung wurde unterbrochen", Toast.LENGTH_SHORT).show();
Log.d("INTERNET_TAG","Internetverbindung war kurrzeitig getrennt");
}
wl.release();
public void SetAlarm(Context context) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(REPEAT, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
if(Build.VERSION.SDK_INT < 23){
if(Build.VERSION.SDK_INT >= 19){
am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
else{
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
}
else{
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10, pi);
}
public boolean isConnectedToInternet(){...}
}
This is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nymvno.hiob.prototyp_v30">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.nymvno.hiob.prototyp_v30.AlarmManagerBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="alarm" />
</intent-filter>
</receiver>
</application>
</manifest>

Push notification not working - Android

I am trying to send a push notification on a button click event. But don't know why, but it is not working. It doesn't show any error though. Can anybody find any error in the code?
public class Verify extends AppCompatActivity {
public static String TAG=Verify.class.getSimpleName();
NotificationCompat.Builder notification;
private static final int uniqueID=12345;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify);
notification=new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
}
public void onVerify(View v)
{
this.defineNotification();
//other code follows
}
public void defineNotification()
{
notification.setContentTitle("Successfully Signed Up");
notification.setContentText("Hi, you just Signed Up as a Vendor");
notification.setWhen(System.currentTimeMillis());
Intent intent=new Intent(this,OtherActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(uniqueID,notification.build());
//Log.i(TAG, "coming here in notification");
}
Here is the Android Manifest Code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sachinparashar.xyz">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SignUp"
android:label="SignUp"
android:theme="#style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Verify"
android:label="#string/title_activity_verify"
android:theme="#style/AppTheme"/>
<activity
android:name=".VendorDetails"
android:label="Vendor Details"
android:theme="#style/AppTheme"/>
<activity
android:name=".OrderTypes"
android:label="Orders"
android:theme="#style/AppTheme" />
</application>
change this :
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
with this :
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Insted of
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
use
PendingIntent pendingIntent=PendingIntent.getActivity(Verify.this,0,intent,0);

Categories

Resources