In my code alarm manger is not working.Rest of my application is working well.Please see my code.
Intent myIntent = new Intent(getApplicationContext(), AndroidAlarmService.class);
myIntent.putExtra("class", "home");
PendingIntent pendingIntent = PendingIntent.getService(this, 0,myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),6000,pendingIntent);
and my android AlarmService class:-
public class AndroidAlarmService extends BroadcastReceiver implements URLs{
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("BroadCast\n");
String name=intent.getStringExtra("class");
if(name.equals("home")){
Intent homeIn=new Intent(context,Home.class);
context.startActivity(homeIn);
}
}
}
in manifest I have done this;
<receiver android:name=".AndroidAlarmService" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
Why its not working??
I got the answer.I made following changes:
Intent myIntent = new Intent(getApplicationContext(), AndroidAlarmService.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,myIntent, 0);
In my AndroidAlarmService class:
Intent homeIn=new Intent(context,Home.class);
homeIn.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(homeIn);
I had the same problem until I found that I had put my Broadcast Receiver on a different package, not the general.
Simply changed:
<receiver android:name=".AndroidAlarmService" android:enabled="true" >
for:
<receiver android:name="com.MyCompany.MyPackage.AndroidAlarmService" android:enabled="true" >
Have you tried changing the
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),6000,pendingIntent);
Have you tried changing the 6000 to something else? It seems like you have everything else correct.
EDIT:
Make sure you have the Read_phone_state permission in your manifest.
Related
I have implemented a service to run with the alarm manager.
I read this link: Should I use android: process =“:remote” in my receiver?
I thought this would be a nice feature for my app, since i want the service to keep running after my app is down.
But when i add this line of configuration to my receiver on the manifest, my service stops being called.
Any clues?
Here is my receiver declaration:
This works:
<receiver
android:name=".service.MyAlarmReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name=".service.MyAlarmReceiver"></action>
</intent-filter>
</receiver>
This wont'n work:
<receiver
android:name=".service.MyAlarmReceiver"
android:enabled="true"
android:process=":remote"
android:exported="false">
<intent-filter>
<action android:name=".service.MyAlarmReceiver"></action>
</intent-filter>
</receiver>
public class MyAlarmReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Time to start scan service!");
Intent intent = new Intent(context, BeaconFinderService.class);
context.startService(intent);
}
}
This is how i start my alarm manager:
// Construct an intent that will execute the AlarmReceiver
Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(), MyAlarmReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), Constants.BLE_SERVICE_LOOP_TIME, pIntent);
When you configure your service to run in remote mode (android:process=":remote"), you will have to debug the process :remote instead as usual .
Personal bug:
So I was having an exception when trying to access FirebaseUser on the service. When in remote mode, you can't access the FirebaseUser, since your process runs on another context.
I had to pass the user through intent extras when initializing the service.
That was all!
I want to add reminder functionality to my Application.
I have Activity with a Button "Remind in 5 minutes". When I press this Button method setReminderInMinutes calls and in 5 minutes I will get a Dialog Fragment.
public void setReminderInMinutes(int timeInMinutes){
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override public void onReceive(Context context, Intent intent){
ReasonsNonDeliveryDlgFragment cityDlg = ReasonsNonDeliveryDlgFragment.newinstance();
cityDlg.show(getSupportFragmentManager(), CityDlgFragment.TAG);
((Vibrator)getSystemService(VIBRATOR_SERVICE)).vibrate(800);
context.unregisterReceiver(this);
}
};
this.registerReceiver(receiver, new IntentFilter("somemessage"));
PendingIntent pintent = PendingIntent.getBroadcast( this, 0, new Intent("somemessage"), 0 );
AlarmManager manager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 1000*60*timeInMinutes, pintent);
}
It works well until I stay on this Activity, but when I press back Button Activity destroys, onReceive method never fire and I receive exception:
" android.app.IntentReceiverLeaked: Activity MyActivity has leaked
IntentReceiver MyActivity$1#42136488 that was originally registered
here. Are you missing a call to unregisterReceiver()?"
So, is there anyway to register BroadcastReceiver outside my Activity?
P.S. I have tried to register it in manifest file:
<receiver android:name=".receivers.ReminderReceiver">
<intent-filter>
<action android:name="name"/>
</intent-filter>
</receiver>
But still nothing works. What am I doing wrong?
In your case it will not work because in manifest file you given the action as "name" but while creating intent you given the action as "somemessage". So how it works. Give the same action name in manifest and intent.
Change your code like below
<receiver android:name=".receivers.ReminderReceiver">
<intent-filter>
<action android:name="some_action"/>
</intent-filter>
</receiver>
And in setReminder() method
PendingIntent pintent = PendingIntent.getBroadcast( this, 0, new Intent("some_action"), 0 );
AlarmManager manager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 1000*60*timeInMinutes, pintent)
The only way to register reciever outside of Activity - register it inside manifest of application. In you case it should look like following:
<receiver android:name=".receivers.ReminderReceiver" >
<intent-filter>
<action android:name="somemessage" />
</intent-filter>
</receiver>
Important to set name in <intent-filter> same as action of your Intent inside sended broadcast.
Try this out
#Override
public void onResume() {
super.onResume();
// Register mMessageReceiver to receive messages.
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("my-event"));
}
// handler for received Intents for the "my-event" event
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
#Override
protected void onPause() {
// Unregister since the activity is not visible
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
It is not necessary to pass a reference of BroadCastReceiver to pending intent. You can also pass the reference of your activity so that it will be called for you when the pending intent goes of.
If you want to use BroadCastReceiver outside of your activity you can declare it in manifest like this
<receiver android:name=".MyReceiver"
android:exported="true">
</receiver>
Then in your activity do this to register it with alarm manager
PendingIntent pintent = PendingIntent.getBroadcast(this, 0, new Intent(this, MyReceiver.class), 0 );
AlarmManager manager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 1000*60*timeInMinutes, pintent);
here is my manifest
<receiver android:name=".MyCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
and
public class MyCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
some code
}
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
some code
}
}
}
it is works but after sometime that press back button and phone be idle it doesn't work any more
(i added "android.os.Process.killProcess(android.os.Process.myPid());" at the end of my code and now it is better and work for maybe 2 3 hour after last execute)
You can use alarm manger to broadcast receiver after certain interval of time like this.
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);
}
I HAVE to be missing something, I made a test Application Project for myself and this works perfectly there but when I tried to implement my AlarmManager into my main project's fragment it just won't work. Here's my code:
The Method that is in my fragment:
public void schedule()
{
Long time = new GregorianCalendar().getTimeInMillis()+10*1000;
Intent intent = new Intent(getActivity(), AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, 10*1000, PendingIntent.getBroadcast(getActivity(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
}
and here is my AlarmReceiver.class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Alarm Triggered", Toast.LENGTH_LONG).show();
}
}
And also here are the preparations in my Manifest file:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
... >
<receiver android:name=".AlarmReceiver"/>
What am I doing wrong?
Note: It seems that it doesn't even reach the AlarmReceiver
Your code seems to work.
But it the problem is the manifest. the
<receiver android:name=".AlarmReceiver"/>
should be the with the full package name, for example
<receiver android:name="com.example.AlarmReceiver"/>
I've double check it with my app. so you are good to go.
You are using the receiver incorrectly. What you need to do first is to add an action to your receiver's manifest
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="com.example.action.ALARM" />
</intent-filter>
Then, construct the intent with that action:
public void schedule() {
Long time = new GregorianCalendar().getTimeInMillis()+10*1000;
Intent intent = new Intent("com.example.action.ALARM");
...
}
and in your receiver:
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.example.action.ALARM")
Toast.makeText(context, "Alarm Triggered", Toast.LENGTH_LONG).show();
}
This should do the trick.
I"m sure this is something that is simple, but I'm not figuring it out. I'm trying to make a simple repeating alarm and it never gets triggered. What I have is:
private void setupAlarms()
{
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, RepeatingAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(HelloAndroid.this, 0, intent, 0);
GregorianCalendar fifteenSeconds = (GregorianCalendar)Calendar.getInstance();
fifteenSeconds.add(Calendar.MINUTE, 0);
fifteenSeconds.set(Calendar.SECOND, 15);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), fifteenSeconds.getTimeInMillis(), pendingIntent);
}
This is called from the main onCreate call.
My alarm receiver:
public class RepeatingAlarm extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, R.string.hello, Toast.LENGTH_SHORT).show();
}
}
In my manifest, I have added:
<receiver android:name=".RepeatingAlarm" android:process=":remote" />
Any help, much appreciated
Have you added an intent filter to your BroadcastReceiver?
Code might look something like this in your AndroidManifest.xml file:
<receiver android:name=".RepeatingAlarm" android:exported="true">
<intent-filter>
<action android:name="intent id text" />
</intent-filter>
</receiver>
and when creating your intent do something like this:
Intent intent = new Intent("intent id text")
where the "intent id text" can be any string you use to identify your intent. Also Android alarms get reset if you reboot your device so that may be something you need to look into.
Hope this helps.