I am calling broadcast receiver from service through this method.
Intent intent = new Intent(NotifyService.this, AlarmReciever.class);
intent.putExtra("TIME",ttime);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 34324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,timee,pendingIntent);
In Broadcastreceiver page I am receiveing through this method.
Bundle b = in.getExtras();
date = b.getString("TIME");
Problem is In Broadcastreceiver page I am getting null value.
My Question is :-
How to pass data from service to broadcastreceiver ?
In your main activity start your service as
i = new Intent(this, SimpleService.class);
startService(i);
registerReceiver(broadcastReceiver, new IntentFilter(SimpleService.BROADCAST_ACTION));
use send broadcast to send any value from service class to mainactiviy class as
intent.putExtra("textval", s);
sendBroadcast(intent);
and then in your main activity class receive it as
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//do your coding here using intent
}
};
OK i assume the "ttime" is a bundle ( from the code your wrote it seems to make sense)
Try and recieve the data like this
Bundle b= in.getBundleExtra("TIME");
In case the "tttime" is of other format say a String or Int you can get the data in onReceive of Broadcastreceiverlike this
String :
in.getStringExtra("TIME");
Integer :
in.getIntExtra("TIME", -1) // -1 is default value which will be return incase no matching integer found with the key "TIME" in intent "in"
For full details and how to get data from intent of different type refer to android documentation
you missed to do like this:-
Intent intent = new Intent(NotifyService.this, AlarmReciever.class);
sendBroadcast(intent);
You can call pendingIntent Like this:-
PendingIntent operation = PendingIntent.getBroadcast(Service.this,
0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(),5* 60 * 1000, operation);
If you are passing any time data to broadcast class,then you can receive that data in receiver class as
String mTime = NotifyService.time;
Try this. Inform me also.
Related
I would like to setup an IntentService to fire on a timer and manually. In the case that new data is created I would like to 'manually' trigger the IntentService to send the data off immediately. The timer will just run every few minutes to ensure all data has been sent off, if it has not it will send data.
Is there a way to block/lock the IntentService such that if I am already sending data manually and the timer fires the service does not try and submit the same data twice?
I have the following IntentService which will pull an item out of the database and post that info to a server.
public class MyIntentService extends IntentService {
#Override
protected void onHandleIntent(Intent intent) {
Long id = intent.getLongExtra(SERVICE_ID, -1);
// grab stuff out of DB based on id and submit to server
}
}
I have a BroadcastReceiver that fires every so often to start that intent service:
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
//Grab stuff I care about
Long someId = ....;
Intent i = new Intent(context, MyIntentService.class);
i.putExtra(SERVICE_ID, someId);
context.startService(i);
}
}
And I start the broadcast receiver, to fire every 10 minutes
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
intent = new Intent(getApplicationContext(), MyBroadcastReceiver.class);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1001, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 60 * 10, pendingIntent);
I fire manually elsewhere in code based on an event when I have new data to send
// On some event happened manually start intent (not on timer)
Context context = getApplicationContext();
Intent intent = new Intent(context, MyIntentService.class);
intent.putExtra(SERVICE_ID, someId);
context.startService(intent);
Again can I block somehow to ensure there are not multiple MyIntentServices running at the same time, which could be possibly sending the same data?
I need to access the ID of the Pending Intent from the Broadcast Receiver class.
Here is the code of my Main Activity from which I set the Alarm using PendingIntent.
private void setAlarm(Calendar targetCal)
{
Intent alarmintent = new Intent(AddAlarm.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(AddAlarm.this, pen, alarmintent, PendingIntent.FLAG_ONE_SHOT); //where pen is the ID
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), sender);
}
And here is the code of my Broadcast Receiver:
public class AlarmReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
int vibrator = intent.getIntExtra("vibrator", 1);
//PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
//intent to call the activity which shows on ringing
Intent myIntent = new Intent(context, Time_Date.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
//display that alarm is ringing
Toast.makeText(context, "Alarm Ringing...!!!", Toast.LENGTH_LONG).show();
ComponentName comp = new ComponentName(context.getPackageName(),
AlarmService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Can I use Intent.putExtra() to receive the same or any other easy way to get the unique ID to the Broadcast Receiver? Any help will be appreciated.
I don't think the second argument to PendingIntent.getBroadcast() is meant to be used by the component that eventually receives the intent (at least I have found no way to access it). If you want to pass some data along that is specific to your app, just use an extra.
Trying to create multiple Alarms using unique PendingIntent . However I am having trouble with this,
From MainActivity I press a button to set an Alarm, and the code for that is:
public void alarmSet(View view)
{
int idTime = (int) System.currentTimeMillis();
Intent intent = new Intent(MainActivity.this, AddAlarm.class);
intent.putExtra("pendInt",idTime);
startActivity(new Intent(MainActivity.this, AddAlarm.class));
}
Taking System time as unique id I am passing the value to the other activity from which I call the Broadcast to initiate alarm. Code for this Activity is:
Intent receive = getIntent();
pen = receive.getIntExtra("pendInt",0);
And here is the method in which I set the alarm.
private void setAlarm(Calendar targetCal)
{
Intent alarmintent = new Intent(AddAlarm.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(AddAlarm.this, pen, alarmintent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), sender);
}
This works for single alarms , however it doesn't generate multiple alarms. What might be the possible reason? Any help will be appreciated. Do I need to post the Broadcast class as well ?
you are making Intent and putting extra but passing another Intent to startActivity()
just replace this
startActivity(new Intent(MainActivity.this, AddAlarm.class));
to this
startActivity(intent );
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() ;
I am adding some basic alarm functionality to my program via the use of AlarmManager and a BroadcastReceiver class (named AReceiver.java). My problem is that the data I add to the bundle attached to the Intent creating the PendingIntent appears to be lost. The only bundle data I can access in the AReceiver class is a android.intent.extra.ALARM_COUNT=1.
Here is the basic code in the main activity class creating the Intent, PendingIntent and the AlarmManager:
[Code in main activity - Notepadv3]
Intent intent = new Intent(Notepadv3.this, AReceiver.class);
intent.putExtra("teststring","hello, passed string in Extra");
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, pendingPeriodIntentId, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timeOfNextPeriod.getTimeInMillis(), alarmIntent);
[Code in the BroadcastReceiver - AReceiver]
public void onReceive(Context con, Intent arg1) {
Bundle extrasBundle = arg1.getExtras();
Log.d("broadcast","contains teststring = " + extrasBundle.containsKey("teststring"));
Log.d("broadcast","is empty? = " + extrasBundle.isEmpty());
Log.d("broadcast","to string = " + extrasBundle.toString());
}
Debug messages say that contains teststring is FALSE, is empty is FALSE and when outputting the whole bundle, I get the android.intent.extra.ALARM_COUNT=1 value.
Any help would be greatly appreciated.
Cheers,
Tom
You have to change this line
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, pendingPeriodIntentId, intent, 0);
into this
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, pendingPeriodIntentId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
otherwise the data is lost