Start intent inside a broadcastreceiver - android

I have used the following code to start a broadcastreceiver which calls a new intent after its designted time, but the problem is the intent is not being called.
The alarm is called by a button on screen, when the button is clicked after designated time the new intent is expected to start.
private void alarm(){
br = new BroadcastReceiver() {
#Override
public void onReceive(Context c, Intent i) {
Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
}
};
Log.v("ranjith","Inside setup");
registerReceiver(br, new IntentFilter("com.example.ads.test"));
pendingIntent = PendingIntent.getBroadcast( this, 0, new Intent(getApplicationContext(),test.class),0);
alarmManager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
}

First, you are not calling set(), setRepeating(), or setInexactRepeating() on the AlarmManager, to schedule an event to occur that will send your broadcast.
Second, your PendingIntent is incorrect. Use the following to line up with your IntentFilter:
pendingIntent = PendingIntent.getBroadcast( this, 0, new Intent("com.example.ads.test"), 0);
Third, using a dynamically-registered receiver with AlarmManager is rather unusual, as it means that your alarm will only be valid while your application is running.

Related

Get the code that trigger onReceive of Broadcast Receiver

class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
doSth();
}
}
In the main activity:
Intent intentA = new Intent(MainApplication.getAppContext(), Receiver.class);
Intent intentB = new Intent(MainApplication.getAppContext(), Receiver.class);
PendingIntent pendingIntentA = PendingIntent
.getBroadcast(MainApplication.getAppContext(),0,intentA,0);
PendingIntent pendingIntentB = PendingIntent
.getBroadcast(MainApplication.getAppContext(),0,intentB,0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5000,
60000, pendingIntentA);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5000,
60000, pendingIntentB);
In the function doSth() i want to know if this broadcast is triggered whether by intentA or intentB, how can i do that without explicitly putting additional information into the intent?, thanks in advance.
You can't. Because the Intents are exactly the same and because of that, the PendingIntents are also exactly the same. You will need to put something in the Intent to differentiate: either the Intent ACTION, Intent DATA, or some "extras".

I use "Action to make Phone Call" but I need to make a called automatically for 5 or 10 min

This is an example
But I need to make a called for 5 minutes or 10 minute automatically. How I can make it?
Intent in=new Intent(Intent.ACTION_CALL,Uri.parse("0000000000"))
try{
startActivity(in);
}
you can use broadcast receiver and set action as Time change.And set Alarm for every 5 min to send action to broadcast receiver.
Add this code in your activity:
PendingIntent service = null;
Intent intentForService = new Intent(context.getApplicationContext(), YourService.class);
final AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
final Calendar time = Calendar.getInstance();
time.set(Calendar.MINUTE, 0);
time.set(Calendar.SECOND, 0);
time.set(Calendar.MILLISECOND, 0);
if (service == null) {
service = PendingIntent.getService(context, 0,
intentForService, PendingIntent.FLAG_CANCEL_CURRENT);
}
alarmManager.setRepeating(AlarmManager.RTC, time.getTime()
.getTime(), 60000, service);
Create Broadcast Receiver and add code for Make Call in onReceive() of receiver.
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("BroadcastReceiver", "debut receive");
Intent in=new Intent(Intent.ACTION_CALL,Uri.parse("0000000000"))
startActivity(in);
}
}
I hope its helpful to you.
Happy Coding..

Call Activity Method from BroadcastReceiver

I am trying to call from my BroadcastReceiver(ReceiverSchedule) to call a method (CancelSchedules) in my Activity(ViewScheduleActivity). The main problem I have is that it the Activity becomes null on the onReceive. I think I'm simply passing it into the intent wrong. I have tried the following link: Call an activity method from a BroadcastReceiver class.
First there is an alarmanager that will get called at the correct time then in the Receiver I want to cancel all alarms. Here is the code in the Activity to set the alarm (this part will work fine).
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(this, RecieverSchedule.class);
myIntent.setClass(this, recieverSchedule.getClass());
myIntent.putExtra("scheduleJson", gs.toJson(schedule));
myIntent.putExtra("LoginUserAsString", gs.toJson(loginUser));
myIntent.putExtra("PatientAsString", gs.toJson(patientResult));
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, i);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(ViewScheduleActivity.this, "Set One Time Alarm at: "+ hour +":" + min, Toast.LENGTH_LONG).show();
Now to pass the Activity over in my BroadCastReceiver I have:
public void setMainActivityHandler(ViewScheduleActivity main) {
viewScheduleActivity = main;
}
with the declaration on top:
ViewScheduleActivity viewScheduleActivity = null;
Now on create I would like to call the method CancelSchedules which is in my ViewScheduleActivity:
public void CancelSchedules()
{
for (int i =0; i< 10; ++i)
{
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(this, RecieverSchedule.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, i);
alarmManager.cancel(pendingIntent);
}
}
In my onReceive in RecieverSchedule I have:
public void onReceive(Context context, Intent intent) {
viewScheduleActivity.CancelSchedules();
}
The problem is that viewScheduleActivity is null causing me to be unable to call it.
I have the following code to pass over the Activity with ReciverSchedule created as a member in the Activity:
recieverSchedule = new RecieverSchedule();
recieverSchedule.setMainActivityHandler(this);
IntentFilter callInterceptorIntentFilter = new IntentFilter("android.intent.action.ANY_ACTION");
registerReceiver(recieverSchedule, callInterceptorIntentFilter);
The setMainActivityHandler will get called and created, but I suspect that I am passing a new RecevierSchedule rather than the set receiverSchdule. If I am correct, how do I pass receiverSchedule into the Alarmmanager? I thought registerReceiver would fix this issue but maybe the placement is wrong.
Your Activity will likely be long killed and dead by the time the AlarmManager triggers your BroadcastReceiver. When that happens, Android will create a new process for your application, instantiate your BroadcastReceiver and call onReceive(). This is why the variable viewScheduleActivity is null.
As #Athena suggested, you don't need to call back into your Activity in order to cancel alarms.

PendingIntent.getBroadcats don't find an Intent

I'm writing an application with multiple alarms and I want to have an option to cancel them. I'm using AlarmManager and PendingIntent. My problem is that when I want to cancel an alarm, getBroadcast don't find a PendingIntent matching the criteria.
private void turnOnMorningRemider()
{
long time = System.currentTimeMillis() + 60 * 45;
Intent tmpIntent = new Intent(this, AlarmPopUpDialog.class);
tmpIntent.putExtra(getString(R.string.alarm_time_of_day), AlarmPopUpDialog.REQUEST_CODE_MORNING);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(tmpIntent);
// Gets a PendingIntent containing the entire back stack
morningPendingIntent = stackBuilder.getPendingIntent(AlarmPopUpDialog.REQUEST_CODE_MORNING, PendingIntent.FLAG_ONE_SHOT);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP, time, morningPendingIntent);
Toast.makeText(this, "Alarm is on.", Toast.LENGTH_LONG).show();
}
To cancel alarm I use:
private void turnOffMorningReminder()
{
Intent tmpIntent = new Intent(this, AlarmPopUpDialog.class);
PendingIntent pi = PendingIntent.getBroadcast(this, AlarmPopUpDialog.REQUEST_CODE_MORNING,
tmpIntent, PendingIntent.FLAG_NO_CREATE);
if (pi != null)
{
alarmManager.cancel(pi);
Toast.makeText(this, "Alarm is off.", Toast.LENGTH_LONG).show();
}
}
The pi variable is always null, so alarm starts ringing.
The next problem is, when I try to create PendingIntent with getBroadcast (instead of stack builder) my alarm never activates.
morningPendingIntent = PendingIntent.getBroadcast(this, AlarmPopUpDialog.REQUEST_CODE_MORNING, tmpIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Is using the stack builder the reason why i can't find the intent?
I feel stupid. My problem was that my AlarmPopUpDialog extended Activity not BroadcastReceiver class. When I added a class that extened BroadcastReceiver in the middle everything started to work.
morningPendingIntent = PendingIntent.getBroadcast(this, AlarmBrodcastReceiver.REQUEST_CODE_MORNING, tmpIntent, PendingIntent.FLAG_UPDATE_CURRENT);
and
public class AlarmBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Intent i = new Intent(context, AlarmPopUpDialog.class );
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtras(intent);
context.startActivity(i);
}
}
I hope that this answer will help the next poor guy.

Starting up Service with AlarmManager , which already might be running

I need to run a service in time interval, for example every 2 minutes. I register it with AlarmManager, it works fine when the service stops itself before that 2 minutes is up, but there is a great chance it will take more than 2 minutes, in this case I'll need the service to be terminated and start up a new one, how can I do this?
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getApplicationContext(), Sender.class);
PendingIntent pi = PendingIntent.getService(getApplicationContext(), 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),1000 * 30, pi);
Instead of starting service by AlarmManager use broadcast. Set AlarmManager to send some broadcast intent. Create your own BroadcastReceiver that will receive that intent and in onReceive method restart(stop and start) service.
//Start AlarmManager sending broadcast
Intent intent = new Intent(context, MyBroadcastReceiver.class); // explicit
peningIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30 * 1000, pendingIntent);
.
//BroadcastReceiver
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(SynchronizationService.class.getName());
context.stopService(serviceIntent);
context.startService(serviceIntent);
}
}
.
//Register receiver in AndroidManifest.xml in Application tag
<receiver
android:name="com.example.MyBroadcastReceiver" >
</receiver>
You should write a login in onStartCommand itself.
Check if service is running or not using variable.
If its running call stopSelf method on service,then again call startservice for same service.
start a alarm manager services u have to use this code
Intent intent = new Intent(activity.this,Sender.class);
pendingIntent = PendingIntent.getBroadcast(activity.this.getApplicationContext(),1, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),1000 * 30, pendingIntent);
for stop this broadcast receiver use this code
Intent intent = new Intent(activity.this,Sender.class);
pendingIntent = PendingIntent.getBroadcast(activity.this.getApplicationContext(), 1,intent, 0);
pendingIntent.cancel() ;

Categories

Resources