Again i have a problem with Broadcasts..
Fragment:
Intent i = new Intent(context,AlarmReceiver.class);
i.setAction(Intent.ACTION_BOOT_COMPLETED);
int id = (int) alarms_ID;
i.putExtra("_id",id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, i, PendingIntent.FLAG_UPDATE_CURRENT);
Calender calender=...;
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"yap",Toast.LENGTH_SHORT).show();
}
and the receiver in manifest:
<receiver
android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
very simple code but still not working...
This code work in activity maybe the problem is the fragment?
If you need more information just say it.
Related
Needed: Launch application using alarm manager when alarm trigger it launch the application.
Tried:
BroadCast Receiver for receive alarm trigger and launch application but not launching the application.
#Override
public void onReceive(final Context context, Intent intent) {
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
Setting Alarm:
public void startAlarm(Context context) {
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, RestartServiceBroadcastReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), AlarmManager.INTERVAL_HOUR, alarmIntent);
}
Manifest:
<receiver android:name="com.android.cchhaatt.RestartServiceBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver"
android:process=":remote">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
I have following schema BOOT_COMPLETE intent ->BroadcastReciever starts, then starting WakefullBroadcastReceiver -> sending intent to NotificationIntentService.
But my WakefullBroadcastReceiver is never working, if i change schema to BOOT_COMPLETE intent ->BroadcastReciever-> sending intent to NotificationIntentService. everething works perfectly
BroadcastReceiver:
public class BootBroadcastReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (!App.isRunning) {
Log.d("wakefull", "start");
Intent startIntent = new Intent("SOMEACTION");
//startIntent.setAction(Utils.NOTIFY_INTENT);
PendingIntent startPIntent = PendingIntent.getService(context, 0, startIntent, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,
SystemClock.elapsedRealtime() + 3000, 5000, startPIntent);
App.isRunning = true;
}
Log.e("bool",App.isRunning+"");
}
}
WakefulBroadcastReceiver:
public class SimpleWakefulReciever extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("wakefull","received");
Intent service = new Intent(context, NotificationWakefulIntentService.class);
startWakefulService(context,service);
}
}
NotificationIntentService:
public class NotificationWakefulIntentService extends IntentService {
public NotificationWakefulIntentService() {
super("NotificationWakefulIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
Log.d("time",(System.currentTimeMillis()/1000)+"");
SimpleWakefulReciever.completeWakefulIntent(intent);
}
}
Manifest:
<receiver android:name=".broadCastReciever.BootBroadcastReciever" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="START"/>
</intent-filter>
</receiver>
<receiver android:name=".wakefullBroadcastReciever.SimpleWakefulReciever">
<intent-filter>
<action android:name="SOMEACTION"/>
<action android:name="WAKEFUL"/>
</intent-filter>
</receiver>
<service
android:name=".wakefulService.NotificationWakefulIntentService"
android:enabled="true">
<intent-filter>
<action android:name="NOTIFY_INTENT" />
</intent-filter>
</service>
First, get rid of all <intent-filter> elements other than the one wrapping <action android:name="android.intent.action.BOOT_COMPLETED"/>. Never put an <intent-filter> on a component unless you want arbitrary third-party apps to start that component whenever they want to.
Then, replace:
Intent startIntent = new Intent("SOMEACTION");
PendingIntent startPIntent = PendingIntent.getService(context, 0, startIntent, 0);
with:
Intent startIntent = new Intent(context, SimpleWakefulReciever.class);
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, 0);
This fixes your Intent to no longer use the action string and fixes your PendingIntent to use getBroadcast(), since you are trying to trigger a BroadcastReceiver.
Then, replace 5000 with a value of at least 60000, since you cannot have a repeating alarm more frequently than that on Android 5.1+.
I'm trying to trigger a notification every 15 minutes, starting from the time I set. Currently my app notifies only when my application is open or when it is in the recent applications but when i closed my application the notification doesn't work any longer but when i open again my app it will trigger the notification. Based on my research I need a Service. So I have 3 files.
In my MainActivity, i set the alarm
private void scheduleNotification(int week){
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra("notifId", getResources().getInteger(R.integer.notification_id));
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, getResources().getInteger(R.integer.notification_id), notificationIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, week);
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
}
in the MainActivity i call a BroadcastReceiver which is the NotificationPublisher. Here is my NotificationPublisher
public class NotificationPublisher extends BroadcastReceiver {
final String TAG = "NotificationPublisher";
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, NotificationAlarmService.class);
service.putExtra("notifId", intent.getIntExtra("notifId", 0));
context.startService(service);
}
then I start a service by using the BroadcastReceiver which is the NotificationAlarmService, here i build a Notification using NotificationCompat.Builder.
public class NotificationAlarmService extends Service {
. . . .
#Override
public int onStartCommand(Intent intent,int flag, int startId)
{
notificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, MainActivity.class);
pendingIntent = PendingIntent.getActivity(this, intent.getIntExtra("notifId", 0), mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("App");
builder.setContentText("Notify Me");
builder.setAutoCancel(true);
builder.setSmallIcon(getNotificationIcon());
builder.setContentIntent(pendingIntent);
notificationManager.notify(intent.getIntExtra("notifId", 0), builder.build());
return super.onStartCommand(intent, flag, startId);
}
I already tried returning START_STICKY instead of super.onStartCommand I understand that when i used a service it will run in the background even if the user closed the application. I also tried changing this and used getBaseContext() in the service but this it doesn't trigger the notification. I don't know if i'm missing something. Thank you in advance.
EDIT
Here is my Manifest
<receiver android:name=".NotificationPublisher"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".NotificationAlarmService"
android:enabled="true">
</service>
After a week, I finally got it. In my Manifest i put something like this
<receiver android:name=".NotificationPublisher">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name=".NotificationAlarmService"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="NOTIFICATION_SERVICE" />
</intent-filter>
</service>
i've removed the enabled="true" in the receiver and also add QUICKBOOT_POWERON. Then add an action in the service which is NOTIFICATION_SERVICE and call it in my service like this
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Try adding Service in your Manifest file (Same as declaring an Activity) under application tag like this,
<service android:name="com.example.android.NotificationAlarmService">
</service>
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>
I have written next broadcast receiver for BOOT_COMPLETED action
public class FineWeatherBootStarter extends BroadcastReceiver {
PendingIntent pendingIntent = null;
public void onReceive(Context context, Intent intent) {
long firstTime = System.currentTimeMillis();
Intent serviceIntent = new Intent(FineWeather.ACTION_REFRESH);
intent.setType(Weather.CONTENT_TYPE);
pendingIntent = PendingIntent.getService(context, 0,serviceIntent, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC,
firstTime, 30*1000, pendingIntent);
Toast.makeText(context, "STARTED!!!!!!!!!", 5000).show();
}
}
I can see "STARTED!!!!!!!!!" message on boot device, but my service seems like not being started every 30 seconds
Where can be a probleb&
Try altering your manifest
<receiver android:name="MyBootReceiver" android:process=":hascode_process">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>