I can't seem these to get up and running:
public class BackgroundSyncService extends IntentService {
public BackgroundSyncService() {
super("SchedulingService");
}
#Override
protected void onHandleIntent(Intent intent) {
Log.i("asd-service", "lol");
BackgroundSyncBroadcastReceiver.completeWakefulIntent(intent);
}
}
Second class:
public class BackgroundSyncBroadcastReceiver extends WakefulBroadcastReceiver {
private AlarmManager alarmManager;
private PendingIntent alarmIntent;
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, BackgroundSyncService.class);
startWakefulService(context, service);
}
public void setAlarm(Context context){
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, BackgroundSyncBroadcastReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
0, 5000, alarmIntent);
ComponentName receiver = new ComponentName(context, BackgroundSyncBroadcastReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
}
Third class:
public class BackgroundSyncBootReceiver extends BroadcastReceiver {
BackgroundSyncBroadcastReceiver service = new BackgroundSyncBroadcastReceiver();
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
service.setAlarm(context);
}
}
}
Manifest.xml:
<receiver android:name=".BackgroundSyncBroadcastReceiver"/>
<receiver android:name=".BackgroundSyncBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".BackgroundSyncService"/>
Your receiver for BOOT_COMPLETED will not be allowed to fire until your app has been manually started by the user one time. This is a security feature to prevent apps from running without the user knowing about them.
So, yeah, I forgot to start it from the acivity...
BackgroundSyncBroadcastReceiver service = new BackgroundSyncBroadcastReceiver();
service.setAlarm(this);
Stupid mistake.
Related
I want to show local notification on specific time. So that I am using alarm manager to set pending intent for specific time. But in my case Broadcast/Service is not getting called if application killed by user.
Check the below code and help me out why am not getting notification after application killed.
public class MainActivity extends AppCompatActivity {
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent notifyIntent = new Intent(this,MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast
(MainActivity.this, 1, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+30000, pendingIntent);
}
}
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, MyNewIntentService.class);
context.startService(intent1);
}
}
public class MyNewIntentService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
CommonUtil.showNotification(getApplicationContext());
return START_STICKY;
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false"
/>
<service
android:name=".MyNewIntentService"
android:exported="false"
/>
You can check here for working sample of Alarm with Broadcast receiver.
How to use Android AlarmManager in Fragment in Kotlin?
Previously am using IntentService to trigger the Background services with AlaramManager. Now am changed the IntentService to JobIntentService for trigger Background process to fetch the data from Server. In Some devices JobIntentService not triggering, it will trigger only after open the application.
Set AlaramManager
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi;
AlarmManager am;
pi = PendingIntent.getBroadcast(getActivity(), requestCode, new Intent(getActivity(), PendingCampaignReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
am = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
BroadCastReceiver Class
public class PendingCampaignReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
PendingCampaign.enqueueWork(context, intent);
}
}
<receiver
android:name=".broadcast.receiver.PendingCampaignReceiver"
android:enabled="true"
android:process=":remote" />
JobIntentService Class
public class PendingCampaign extends JobIntentService {
public static void enqueueWork(Context context, Intent intent) {
enqueueWork(context, PendingCampaign.class, PENDING_CAMPAIGN_JOB_ID, intent);
}
#Override
protected void onHandleWork(#NonNull Intent intent) {
//am doing my API calling here
}
}
<service
android:name=".services.PendingCampaign"
android:permission="android.permission.BIND_JOB_SERVICE" />
Is there any issue in my Coding. Help me to solve the issue.
I am using the following code to start a service periodically whether the app is foreground or not .
Intent getAlertIntent = new Intent(getApplicationContext(), GetAlertAlarmReceiver.class);
PendingIntent getAlertpendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, getAlertIntent, 0);
AlarmManager getAlertmanager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
getAlertmanager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, getAlertpendingIntent);
The code for my receiver is following
public class GetAlertAlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, GetAlertServiceIntent.class);
context.startService(i);
}
}
And and service
public class GetAlertServiceIntent extends IntentService {
public GetAlertServiceIntent() {
super("GetAlertServiceIntent");
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
protected void onHandleIntent(Intent intent) {
doSomething();
}
}
In my manifest file I declared everything as following
<receiver
android:name=".broadcastreceiver.GetAlertAlarmReceiver"
android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="com.surroundapps.shotorkoporibar.services.GetAlertServiceIntent" />
This code work as expected in API level below Marshmallow but in marshmallow it doesn't work .
I have the main activity, that execute a service. In this service, I init a BroadcastReceiver (alarm).
Then I need send briadcast from the onReceive method of alarm broadcst, but doesn't work. But if I execute sendBroadcast() of other method in alarm, work perfectly.
See de code for explain me:
Activity (init service and get broadcastReceiver)
private final BroadcastReceiver abcd = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Intent in = getIntent();
//finish();
Log.d("sdasd", "onReceive: BROADCAST RECIBIDO!!!");
}
};
Service
alarm alarm = new alarm();
public void onCreate()
{
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
alarm.setAlarm(this, tiempo);
return START_STICKY;
}
Alarm.class
#Override
public void onReceive(Context context, Intent intent)
{
context.sendBroadcast(new Intent("xyz"));
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
}
public void setAlarm(Context context, int tiempo)
{
Log.e("TAG", "setAlarm: ");
context.sendBroadcast(new Intent("xyz"));
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), tiempo, pi); // Millisec * Second * Minute
}
In alarm.class, the sendBroadcast in serAlarm() is executed, but the sendBroadcast in onreceive, doesn't executed. And the toast work perfectly.
Why?
I guess, you need to regist BroadcastReceiver in Service.class.
You can regist BroadcastReceiver in 2 way;
AndroidManifest.xml use
<receiver android:name=".TestReceiver">
<intent-filter>
<action android:name="xyz"/>
</intent-filter>
</receiver>
Sourcecode :
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("xyz");
registerReceiver(abcd, intentFilter);
private final BroadcastReceiver abcd = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Intent in = getIntent();
//finish();
Log.d("sdasd", "onReceive: BROADCAST RECIBIDO!!!");
}
};
======
".TestReceiver" example: It is Java Class File
public class TestReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
}}
Strange issue, my app has tens of thousands of users, for most of them this works perfectly. Some, let's say 5%, experience issue that causes reminders to not be fired.
I've put logging on the scheduling mechanism, including the scheduled timestamp, so I know that the reminder was scheduled, but it AlarmReceiver wasn't called.
Why would AlarmManager sometimes not fire?
ReminderLogic.java -
public class ReminderLogic {
public static void schedule() {
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
int timeMillis = ... // some calculation
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeMillis, alarmIntent);
else
alarmManager.set(AlarmManager.RTC_WAKEUP, timeMillis, alarmIntent);
}
}
AlarmReceiver.java -
public class AlarmReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, AlarmService.class);
startWakefulService(context, service);
}
}
AlarmService.java -
public class AlarmService extends IntentService {
private final static String TAG = "AlarmService";
public AlarmService() {
super(TAG);
}
#Override
protected void onHandleIntent(Intent intent) {
ReminderLogic.schedule();
AlarmReceiver.completeWakefulIntent(intent);
}
}
BootReceiver.java -
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ReminderLogic.schedule();
}
}
PackageReplaceReceiver.java -
public class PackageReplaceReceiver extends BroadcastReceiver {
private static final String TAG = "BootReceiver";
#Override
public void onReceive(Context context, Intent intent) {
ReminderLogic.schedule();
}
}
AndroidManifest.xml -
<service android:name=".reminders.AlarmService" />
<receiver android:name=".reminders.AlarmReceiver" />
<receiver
android:name=".reminders.BootReceiver"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".reminders.PackageReplaceReceiver"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>