I am creating an service which is use Service Component ,I want to run the application always in background . Suppose i switch off the mobile and when i on the mobile our application is closed i.e. automatically.
am trying this code
androidManifest.xml
<receiver android:name=".receiver.ConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and adding this code in BroadCastreceiver Class
update
private class ConnectionReceiver extends BroadcastReceiver{
private Timer mTimer;
private TimerTask mTimerTask;
private long interval;
#Override
public void onReceive(Context context, Intent intent) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, ConnectionReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);
{
calGps();
}
}
Hi Leema Rose
You want to launch your service/activity after the device booted. Check the below links you will understand how to use BroadcastReceiver.
Autostart an application at bootup
Start Service at boot
I hope it may help you.
New Answer:
Add <category android:name="android.intent.category.HOME" /> in intent-filter of receiver's manifest file.
You have to add a manifest permission entry:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
(of course you should list all other permissions that your app uses).
Then, implement BroadcastReceiver class, it should be simple and fast executable. The best approach is to set an alarm in this receiver to wake up your service (if it's not necessary to keep it running ale the time as Prahast wrote).
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, MyService.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);
}}
Then, add a Receiver class to your manifest file:
<receiver android:enabled="true" android:name=".receivers.BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Related
I've used an alarmManager to send daily notification. Since alarmManager stops after restarting phone so I've created a BroadcastReceiver to trigger on BOOT_COMPLETE, still no success.
Even not getting toast.
BroadcastRreceiver class
package com.aman.dailynoti;
import...
public class BReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ((intent.getAction()).equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "broadcast", Toast.LENGTH_SHORT).show();
SharedPreferences mpreferences=context.getSharedPreferences("myPreferences",MODE_PRIVATE);
int h=mpreferences.getInt("hour",14);
int m=mpreferences.getInt("minute", 30);
Calendar calendar= Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,h);
calendar.set(Calendar.MINUTE,m);
calendar.set(Calendar.SECOND,00);
Intent notiIntent = new Intent(context, Notification_Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, notiIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application...>
<receiver
android:name="com.aman.dailynoti.BReceiver"
android:enabled="true"
android:label="breceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
Dont quote me on this but I believe android only gives a small window of time for your applications to do what they need to do on boot. If you want the user to receive notifications when not using the app look into push notifications.
you should use also intent filter android.intent.action.QUICKBOOT_POWERON to recieve after restart
I have an AlarmManager that shows a Toast every 10 mins.but when os kill apps in the background , so my AlarmManager not work any more. what I have to do?
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context con, Intent arg1) {
Global.ShowMessage(con, Global.GetCurrentDateTime());
}
}
In Manifest.xml :
<receiver android:name=".MyReceiver" > </receiver>
in main Activity :
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
long time = cal.getTimeInMillis();
Intent i = new Intent(this, MyReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 9854, i, PendingIntent.FLAG_UPDATE_CURRENT);
// am.set(AlarmManager.RTC_WAKEUP,time,pi);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, 600 * 1000, pi);
From the official documentation on the lifecycle
The system never kills an activity directly. Instead, it kills the process in which the activity runs, destroying not only the activity but everything else running in the process, as well.
But you can create a Service that will not be killed with the Activity. To be more specific, you want a Service that run in foreground, this will not be killed by the system as stated in the Services documentation
For a specific example, I prefer to let Google guide you with this [example](https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)).
Append this code in manifest.
<receiver android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I have used broadcast receiver with alarm manager to hit webservices for every 60 seconds,seems its working fine when i have used my broadcast receiver in different class but its not calling when i declare a receiver with in my activity
my code is below
public static void startAlarm(Context context) {
Intent locationAlarm = new Intent(context, GetLocation.class);
AlarmManager alarms = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Calendar updateTime = Calendar.getInstance();
PendingIntent recurringAlarm = PendingIntent.getBroadcast(context, 0,
locationAlarm, PendingIntent.FLAG_CANCEL_CURRENT);
alarms.setRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis() + 500, UPDATE_INTERVAL,
recurringAlarm);
}
Broadcast receiver :
public class GetLocation extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
LogUtil.d("Started Service");
}
}
My Manifest file :
<receiver android:name="com.sample.sample.listeners$GetLocation" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
kindly correct my mistake. my Started service method never called
You have a typo in your manifest file. You have put $GetLocation instead of .GetLocation
<receiver android:name="com.sample.sample.listeners.GetLocation" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
In my manifest file I have declared the receiver. (as follows)
<receiver android:name=".OnAlarmReceive" />
however, once I shut down my application, I am not able to get the alarms and the notifications. Apparently, a call to the OnReceive in my Broadcast receiver is never made.
public class OnAlarmReceive extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent arg1)
{
//various stuff
}
}
Inside the MainActivity, my alarm manager class is as the follows.
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent("MY_ALARM_NOTIFICATION");
intent.setClass(this, OnAlarmReceive.class);
intent.putExtra("message", message);
PendingIntent pendingIntent = PendingIntent
.getBroadcast(MainActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar timeCal = Calendar.getInstance();
timeCal.set(Calendar.HOUR_OF_DAY, hour);
timeCal.set(Calendar.MINUTE, minutes);
alarmManager.set(AlarmManager.RTC_WAKEUP, timeCal.getTimeInMillis(), pendingIntent);
and my manifest as is follows :
<receiver android:name=".OnAlarmReceive">
<intent-filter android:priority="1">
<action android:name="MY_ALARM_NOTIFICATION"/>
</intent-filter>
</receiver>
What should I do in order to receive the notifications/alarms even if I have shut off my app. Background service ?
you should add intent-filter in manifest,as
receiver android:name=".SmsBroadCastReceiver">
<intent-filter android:priority="20">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
As Gong Cong says, you need to declare which events your receiver should listen.
For example :
<receiver android:name=".OnAlarmReceive">
<intent-filter>
<action android:name="MY_ALARM_NOTIFICATION"/>
</intent-filter> </receiver>
and then when your set your alarm, use an intent with your action :
Intent intent = new Intent("MY_ALARM_NOTIFICATION");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pi = PendingIntent.getBroadcast( this, 0, intent, 0 );
Your code is working fine!
All you have to do is to change this line:
alarmManager.set(AlarmManager.RTC_WAKEUP, timeCal.getTimeInMillis(),
pendingIntent);
With this line:
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 5000, pendingIntent);
And the code in the "onReceive" will run after 5000ms (5sec) even when app is not running
In My understanding, In some cased depending on the way of implementations, OS has authority to adjust the alarm set time. So try to use AlarmManager.set(...), AlarmManager.setexact(...) etc accordingly. In Some cases, depending on the manufacturer(Custom Android OS), there is a possibility that the OS is blocking fire alarm.
Adding android:exported="true" for receiver in manifest file helped me to receive alarms (and thus, wake the application) even when application was shut-down (intentionally by me, removing app from task list).
1.Declare the receiver in the Manifest-file:
<receiver android:name="your.package.name.TestAlarmReceiver"></receiver>
Always remember that the whole Android-System is case sensitive. So check your spelling is correct in the AndroidMainfest.xml.
2.If you create a PendingIntent for your Receiver, please add an requestCode - even it is a random number! Without your onReceive code never get called!
The function which start AlarmManager should look like below:
public static void scheduleTestAlarmReceiver(Context context) {
Intent receiverIntent = new Intent(context, TestAlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 123456789, receiverIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+startDelay, someDelay, sender);
}
BroadcastReceiver class:
package your.package.name;
public class TestAlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
// your code here!
}
}
The original article: Why my BroadcastReceiver does not get called?
I have the following problem. I have 2 classes. 1 is called AlarmService and the other is called TimeAlarm which extends BroadcastReceiver.
The App should do the following thing: It should generate a new Alarm to a time specified in the preferences (Which it already does...) also in Logcat i can see how the Alarm gets triggered. But the problem is, that the Notification which should be shown does not show up in the StatusBar.
Here is all the code i have for this:
AndroidManifest.xml:
<receiver android:name="com.ikalma.alarmmanager.TimeAlarm">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
AlarmService.java:
private Context context;
private PendingIntent mAlarmSender;
public AlarmService(Context context) {
this.context = context;
Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
notifyIntent.setClass(context, myActivity.class);
mAlarmSender = PendingIntent.getBroadcast(context, 0, notifyIntent, 0);
}
public void startAlarm(int stunde, int minute) {
Calendar updateTime = Calendar.getInstance();
updateTime.set(Calendar.HOUR_OF_DAY, stunde);
updateTime.set(Calendar.MINUTE, minute);
updateTime.set(Calendar.SECOND, 00);
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, mAlarmSender);
}
TimeAlarm.java:
#Override
public void onReceive(Context context, Intent intent) {
Log.e("TEST", "onReceive() called...");
}
The Receiver in the Manifest is inside of the Tag so that should not be a problem.
The problem is, that if i restart my device, it gets called. But not if an Alarm gets triggered.
But the onReceive() method should also be called if an alarm gets triggered, shouldn't it?
Thanks for your help!
your intent filter is only listening to boot complete intents and not your own alarm broadcast intent action. update your intent filter so that your broadcast intent is also received (that means for your special case add the action of Intent.ACTION_MAIN to your intent filter)
<receiver android:name="com.ikalma.alarmmanager.TimeAlarm">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</receiver>