Broadcast receiver and pending intent : Show a toast - android

The following is the code for an alarm that has to hit the BroadCast Receiver :
Intent intentWithData = new Intent(context, TokenActivity.class);
intentWithData.putExtra(Constants.ID,id);
intentWithData.putExtra(Constants.POSITION, finalI);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 007, intentWithData, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
The code for the Broadcast receiver is
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class TokenBroadcastReceiver extends BaseBroadCastReceiver {
String Id;
int position;
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Create a toast", Toast.LENGTH_SHORT).show();
}
}
The manifest is :
<receiver android:name=".broadcastReceiver.TokenBroadcastReceiver"/>
The toast is not showing up. Where am I going wrong with this code?

You're mixing 2 things.
If you want your receiver to get the intent:
Intent intentWithData = new Intent(context, TokenBroadcastReceiver.class);
intentWithData.putExtra(Constants.ID,id);
intentWithData.putExtra(Constants.POSITION, finalI);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 7, intentWithData, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
if you want your activity to get the intent:
Intent intentWithData = new Intent(context, TokenActivity.class);
intentWithData.putExtra(Constants.ID,id);
intentWithData.putExtra(Constants.POSITION, finalI);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 7, intentWithData, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
Plus, make sure your receiver is registered in your AndroidManifest.xml

You are setting the pending intent to open an activity as per your code
Intent intentWithData = new Intent(context, TokenActivity.class);
and displaying the toast in broadcast receiver. Please correct your code and it will start working.
Intent intentWithData = new Intent(this, TokenBroadcastReceiver.class);
intentWithData.putExtra("id",5);
intentWithData.putExtra("position", 4);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 007, intentWithData, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
Don't forget to register your broadcast in manifest
<receiver android:name=".broadcastReceiver.TokenBroadcastReceiver"/>

Related

How to disable running Alarm Reciever?

I set my alarm receiver as follow:
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("alarmId", REQUEST_CODE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
time * 1000, pendingIntent);
Then I am canceling it from another class:
Intent myIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)activity.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
But it does not work. Please help me!
Your Intents are not equivalent. The first one uses REQUEST_CODE. The second one uses 0. You need to make these be the same.

I can't set repeating of AlarmManager using setExact()

I tried to set repeating AlarmManager using setExact. In activity:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.setAction(ACTION_ALARM_RETRY);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, 7000, sender);`
And in receiver after my operations I have:
public void onReceive(Context context, Intent intent){
//Here are my operations...
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
intent.setAction(ACTION_ALARM_RETRY);
PendingIntent sender = PendingIntent.getBroadcast(context, 1, intet, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, 7000, sender);
}
But it repeats all time without foggiest pause...
Help please!

Start app at a specific time in Android

I want to display a notification from my application at a specific time say 6.30 am daily. I have successfully done that. Following is the code which executes at specified time. However the code works only if the app is in open state or put to background. It does not work if I reboot the device and don't launch the app, also does not work if I kill the app .
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent;
alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, MyReceiver.class);
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,6);
calendar.set(Calendar.MINUTE,30);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
alarmIntent);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
Following is the receiver BroadCast method
#Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
The alarm service class displays a notification.
/...Display notification.../

Pending intent is not working

I am using the pending intent in activity.
Intent intent = new Intent(this, MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 34433,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (5000), pendingIntent);
here i am going from one activity(MainActivity) to another activity(MyActivity).
but it is not working.
if i try to do it with simple Intent then it works fine.
You have requested a PendingIntent to start a BroadcastReceiver, but you are trying to start an Activity. This can't work.
If you want to start an Activity, instead of this:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 34433,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
do this:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 34433,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
You can also use broadcast receiver with PendingIntent to start another activity.
create another class for example receiver as sub class of the BroadcastReceiver
public class receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
// code to start another activity
Intent intent = new Intent(context.getApplicationContext(), MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
and define MyActivity as receiver in android menifest file
and in main activity change this code
MyActivity.class to receiver.class
Intent intent = new Intent(this, receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 34433,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (5000), pendingIntent);

Android : AlarmManager not start

I must create an AlarmManager that repeating every seconds, I use this code
Intent in = new Intent(context,Tempo_Indietro.class);
in.putExtra("id_widget", appWidgetIds[i]);
PendingIntent pi = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000, pi);
but it don't start...why ??
To start a Scheduled Activity: you can use like:
Step1: Setting for AlarmManager
Intent intent =new Intent(context,AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManger.RTC_WAKEUP,System.currentTimeMillis(), 1000, pi);
Step2: creating a BroadcastReceiver
public class AlarmReceiver extends BroadcastReceiver {
//override onReceive(Context, Intent) method
#Override public void onReceive(Context context, Intent intent)
{
//.........
Intent i = new Intent(context,Tempo_Indietro.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
//..........
}
}
Note: Don’t forget to include the newly created activity, receiver in the AndroidManifest.xml file.

Categories

Resources