After i restart or turn on my device my receivers don't working.
When i don't restart or turn on my device they are working...
Manifest:
<receiver
android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".ActionReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
String msg=intent.getStringExtra("msg");
String note=intent.getStringExtra("note");
Intent startIntent = new Intent(context, alarmDialog.class);
startIntent.putExtra("msg",msg);
startIntent.putExtra("note",note);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);
}
Activity:
Intent intent = new Intent(addOne.this,AlarmReceiver.class);
intent.putExtra("msg", title.getText().toString());
intent.putExtra("note", note.getText().toString());
AlarmManager alarmMgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
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+.
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.
I am trying to setup an alarm at a specified time, but it is not being caught in my reciver.
Setup:
Intent intent = new Intent(this, ActionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar current = Calendar.getInstance();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (current.getTimeInMillis() + 60000),3600000, pendingIntent);
Here is my reciver:
public class ActionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras(); //breakpoint here that doesn't get triggered
}
}
I have put these values in my manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name="com.project.ActionReceiver" android:enabled="true" />
Not sure what is wrong... thanks!
Finally got the receiver to fire! I added the following code to my manifest:
<receiver
android:name="com.project.ActionReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.project.ActionSetter" >
</action>
</intent-filter>
</receiver>
Found here with details: https://stackoverflow.com/a/16119351/1174574
The name of receiver in your manifest should be class name, such as:
<receiver android:name="com.project.ActionReceiver">
BTW, set an action is a better practice.
Intent intent = new Intent(this, ActionReceiver.class);
intent.setAction("com.project.action.ALERM");
And in the manifest
<receiver android:name="com.project.ActionReceiver">
<intent-filter>
<action android:name="com.project.action.ALERM"/>
</intent-filter>
</receiver>
Try changing the android:name attribute of your receiver to the fully qualified class name of your ActionReceiver. Something like:
<receiver android:name="com.project.ActionReceiver" android:enabled="true" />
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>