Start service for every few seconds - android

I want to start a service when wifi network connected.
If I start the service when wifi connected, by using
context.startService(intent_alarm);
it works fine.
I want to start the service for every 10 seconds after recieving broadcast. So I have used AlarmManager
Here is the code:
public class NetworkChangeReceiver extends BroadcastReceiver{
public static AlarmManager am;
public static PendingIntent sender;
#Override
public void onReceive(final Context context, final Intent intent) {
Intent intent_alarm = new Intent(context, MyService.class);
sender = PendingIntent.getBroadcast(context, 0, intent_alarm, 0);
am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long l = System.currentTimeMillis();
l += 3600L;
am.setRepeating(AlarmManager.RTC_WAKEUP,l, 3600L, sender);
//context.startService(intent_alarm);
}
}
I think here I gave 3.6 seconds as the intervel.
But the service not running, I checked it manually.
Please tell me what I'm doing wrong?

You need to tell the alarm manager to start your service:
Intent intent_alarm = new Intent(context, MyService.class);
sender = PendingIntent.getService(context, 0, intent_alarm, 0);

you have used PendingIntent.getBroadcast instread of PendingIntent.getService
so use this - >
Intent intent_alarm = new Intent(context, MyService.class);
sender = PendingIntent.getService(context, 0, intent_alarm, 0);

Related

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..

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() ;

Why an Alarm Broadcast receiver not receiving broadcast?

I am trying to start a service at specific intervals with help of a BroadcastReceiver. I have defined the receiver as an inner class in service itself as follows:
public class AlarmReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("RTESPLDebug", "Recieved Broadcast!"); //Never appears in LogCat
//Intent i = new Intent(context, LocationUpdateService.class);
context.startService(intent);
}
};
I register the alarm receiver in service's onCreate():
context = super.getApplicationContext();
AlarmReceiver ar = new AlarmReceiver();
IntentFilter intfilter = new IntentFilter(Intent.ACTION_DEFAULT);
intfilter.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(ar, intfilter);
I first start the service from my first activity and then register a single alarm there:
Intent i = new Intent(context, LocationUpdateService.class);
context.startService(i);
// Schedule first alarm
int nextUpdateInterval = 30*1000; // Let first alarm be after 30 sec
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(context, LocationUpdateService.class);
intent.setAction(Intent.ACTION_DEFAULT);
intent.addCategory(Intent.CATEGORY_DEFAULT);
PendingIntent pintent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+nextUpdateInterval, pintent);
In the service I register for a single location update, and when I receive update (I do receive), I process it (I have debugged this, processing indeed completes without errors), and then I register next alarm in similar way as above:
int nextUpdateInterval = getNextUpdateInterval(); // Returns 30000
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(context, LocationUpdateService.class);
intent.setAction(Intent.ACTION_DEFAULT);
intent.addCategory(Intent.CATEGORY_DEFAULT);
PendingIntent pintent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+nextUpdateInterval, pintent);
Lots of code and a long questions.

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.

android : all of alarm were cleared how to get them back

first of all , i would like to apologize for my English its bad
all of alarm that i create by this class
Intent intent = new Intent(SETALARM.this, ALARMRECEIVER.class);
intent.putExtra("pk", pk);
sender = PendingIntent.getBroadcast(this, pk, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),60000, sender);
were cleared when device is shut off
what should i do to restore all of alarm back
thank you very much for your help
edit
here is receiver class
#Override
public void onReceive(Context context, Intent intent) {
WakeLocker.acquire(context);
pk = Integer.parseInt(intent.getExtras().get("pk").toString());
Intent intent2 = new Intent(context,ALERT.class);
intent2.putExtra("pk", pk);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
WakeLocker.release();
}}
If you mean that you lose the alarms when the device is turned off then this issue has been addressed well here
https://stackoverflow.com/a/5439320/374866

Categories

Resources